Archive

Posts Tagged ‘object’

Paamayim Nekudotayim In PHP

January 30th, 2009 Tech No comments

What? Don’t worry, I can’t say it either. It is officially called the Scope Resolution Operator (but also just a double colon) and is used to reference static properties and functions of a class. It is also used to reference overridden functions of classes.

To reference a constant of a class you do something similar to the following.

class MyClass {
 const CONST_VALUE = 'A constant value';
}
 
echo MyClass::CONST_VALUE;

To call a static function or a parameter you need to include the word static in the function or parameter definition. You can then reference this function through the scope resolution operator.

class MyClass {
 public static $my_static = 'static var';
 
 public static function thisIsFunction() {
 
 }
}
 
echo MyClass::$my_static; // prints 'static var'
MyClass::thisIsFunction(); // calls thisIsFunction() in MyClass

You can also use the scope resolution operator to reference functions and parameters in parent classes. This is accomplished by using the parent operator. The following code has two class definitions, one of which basically exists to call the function from the parent.

class MyClass {
 public static $my_static = 'static var';
 
 public static function thisIsFunction() {
 
 }
}
 
class ChildClass extends MyClass{
 
 public static function childFunction() {
  parent::thisIsFunction();
 }
}
 
OtherClass::childFunction(); // calls childFunction() in MyClass

The call to the childFunction() function basically calls the function thisIsFunction() in the parent class. This is useful if you want to override the parent function, but still use most of the basic functionality. For example, the child class could take in a parameter, which it then formats or alters and passes this to the parent class.

Remembering Authenticated Sessions With Zend Framework

January 27th, 2009 Tech No comments

After setting up your session management in your application using one of the Zend_Auth adapters you might want to allow users to stay logged in. What you need to do is set some configuration options in the Zend_Session object. Zend_Auth uses Zend_Session as an object orientated way of manipulating the $_SESSION variable. Any changes you make to the Zend_Session object will affect the Zend_Auth object, as long as you set these options before the sessions are started.

There are a number of configuration options available, but for the effect I was looking for I only needed to change the ones below. You might not need to set all of these, but it gave me the best cross browser behavior.

[live]
sessions.name = SESS_NAME
sessions.strict = off
sessions.use_only_cookies = on
sessions.cookie_lifetime = 12345678
sessions.remember_me_seconds = 12345678
sessions.gc_maxlifetime = 12345678

12345678 is the number of seconds, which is about 6 months.

You can load these configuration options into your session using the following code.

// load the config file
$configuration = new Zend_Config_Ini('config.ini', 'live');
// load the config file into the session options.
Zend_Session::setOptions($configuration->sessions->toArray());

Put this in your bootstrap file so that it is loaded before your Zend_Auth call. You should now find that your users are able to close down the browser and reopen it with the session intact.

External Script Files And Printing Objects With Flex

November 6th, 2008 Tech No comments

Yesterday I talked about using the Flex Script element to run code within the mxml file, you can also use the source attribute of the Script element to reference external files. To create an external script file FlashDevelop go to the File->New and select Blank Document. You can also do this by pressing Ctrl+N. This will create a blank document that you must save into your src folder of your project with the extension as. Note that if you call this file sourcefile.as then you must reference this in your script tag like this.

<mx:Script source="sourcefile.as">

</mx:Script>

When you do this you will notice that the script file will be part of the Main.mxml file. You can add functions in the same way as you would with an inline script tag.

Printing

With this external script file we will now create some images and print them using the built in printing object that Flex has. This object is called FlexPrintJob and you must include this at the top of your script file if you want to use it. Put the following line at the top of your script file.

import mx.printing.FlexPrintJob;

Create a button in your mxml file that will be used to print.

<mx:Button id="print" label="Print" click="printImage();" />

Add in an image that will be printed.

<mx:Image source="one.png" id="one" />

Here is the image that will be used.

Flex test image

Flex test image

Go back into the script file and add the function that will print the image out. We called this function printImage() in our Button declaration. This function works by creating a new FlexPrintJob object called printJob. This function start() is then used to try and print. Basically, this function returns true if the print job worked, and false if the user clicked cancel.

If the start() function returns true then the object to be printed (in this case the image with the id of "one" is added to the printJob object. Finally, the send() function is called which sends the data off to the printer. Here is the function.

public function printImage():void
{
 // Create a FlexPrintJob instance.
 var printJob:FlexPrintJob = new FlexPrintJob();
 
 // Start the print job and check that it worked
 if (printJob.start() != true) {
  return;
 }
 
 // add object to be printed to the print job
 printJob.addObject(one);
 
 // send to printer
 printJob.send();
}

This is the simplest use of the FlexPrintJob function as there are controls that can be used to format the pages.

It is possible to print off multiple objects, especially if they are children to another object, by adding the parent object to the FlexPrintJob object. Take the following section of mxml, which defines a Canvas element, with three positioned images as children.

<mx:Canvas id="images">
 <mx:Image source="one.png" id="one" x="0" y="0" />
 <mx:Image source="two.png" id="two" x="100" y="0" />
 <mx:Image source="three.png" id="three" x="0" y="100" />
</mx:Canvas>

This produces the following application.

Three images in a canvas

Three images in a canvas

To print out all three images at once just include the Canvas object instead of the single image object. Change line 11 in the printImage() function from this:

 printJob.addObject(one);

To this.

 printJob.addObject(images);

The printJob object knows about inheritance and will print out everything contained within that element. This includes buttons or any other element that you put into the canvas element.

See the Flex documentation for more information about the FlexPrintJob object.

Serialize And Unserialize With PHP

June 30th, 2008 Tech No comments

If you have an object or array that you want to save until a later you can use the serialize() and unserialize() functions. The operation of the functions are straightforward. To serialize() an array just pass the serialise function the array like this.

$array = array(1,2,3,4);
$serializedArray = serialize($array);

Now when we print the serialized array out we get the following.

a:4:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;i:4;}

This contains all of the data needed to recreate our array. Be careful not to edit this string because it will not work if you want to unserialize it and get the array back. You can store this string in a file or a database so that you can recreate the exact same array at a later date.

If you do chose to store the serialized string in a database you might want to encode it as well. This is to stop the database encoding or removing any characters and destroying your array. To encode the string you can use the base64_encode function in this way.

$encodedSerializedArray = base64_encode(serialize($array));

This string now contains the following.

YTo0OntpOjA7aToxO2k6MTtpOjI7aToyO2k6MztpOjM7aTo0O30=

To get the array back again we use the unserialize() function like this. If you used the base64_encode() to encode the array then you can use the base64_decode() function to turn it back into an unserializable string again.

$array = unserialize($array);

The $array variable now contains our original array.

The same can also be done with objects in exactly the same way. The main difference is that PHP will try to call the __wakeup() function of the object (if it exists) once it has been reconstructed.

Getting All Form Objects In Access VBA

January 10th, 2008 Tech No comments

In Access all forms are contained as objects in the AllForms collection, which is part of the Application.CurrentProject object. To iterate through them and print them off just load them one by one into a temporary object and use the Name property of the form object to print off its name.
Sub AllForms()
  Dim obj As AccessObject, dbs As Object
  Set dbs = Application.CurrentProject
  ' Search for open AccessObject objects in AllForms collection.
  For Each obj In dbs.AllForms
    ' Print name of obj.
    Debug.Print obj.Name
  Next obj
End Sub

If you want to get a list of all of the active form objects (ie. those forms that are currently open) then use the IsLoaded property for each object.
Sub AllForms()
  Dim obj As AccessObject, dbs As Object
  Set dbs = Application.CurrentProject
  ' Search for open AccessObject objects in AllForms collection.
  For Each obj In dbs.AllForms
    If obj.IsLoaded = True Then
      ' Print name of obj.
      Debug.Print obj.Name
    End If
  Next obj
End Sub