Page 251 - Beginning PHP 5.3
P. 251

Chapter 8: Objects
                           If the PHP engine can ’ t find an  __autoload()  function, or if your  __autoload()  function fails to load
                         the   Person  class, the script exits with a  “Class  ‘ Person ’  not found”  error.



                           Storing Objects as Strings

                           Objects that you create in PHP are stored as binary data in memory. Although you can pass objects
                         around using PHP variables, functions, and methods, sometimes its useful to be able to pass objects to
                         other applications, or via fields in Web forms, for example.

                           PHP provides two functions to help you with this:
                            ❑       serialize()  converts an object  —  properties, methods, and all  —  into a string of text

                            ❑       unserialize()  takes a string created by  serialize()  and turns it back into a usable object
                            The following example shows these two functions in action:

                             class Person {
                               public $age;
                             }

                             $harry = new Person();
                             $harry- > age = 28;
                             $harryString = serialize( $harry );
                             echo “Harry is now serialized in the following string: ‘$harryString’ < br / > ”;
                             echo “Converting ‘$harryString’ back to an object... < br / > ”;
                             $obj = unserialize( $harryString );

                             echo “Harry’s age is: $obj- > age < br / > ”;
                           This code creates a simple  Person  class with one property,  $age . It then creates a new  Person  object,
                           $harry , and sets its  $age  property to  28 . It calls  serialize()  to convert the object to a string, which it
                          displays. Finally, it converts the string back into a new object,   $obj , then displays its  $obj - > age

                          property (  28 ). Here ’ s the result of running the script:

                             Harry is now serialized in the following string: ‘O:6:”Person”:1:{s:3:”age”;i
                             :28;}’
                             Converting ‘O:6:”Person”:1:{s:3:”age”;i:28;}’ back to an object...
                             Harry’s age is: 28

                           You can actually use  serialize()  and  unserialize()  on any PHP value, not just objects. However,
                         it ’ s especially useful with objects and arrays, because these structures can be quite complex and it ’ s not
                         easy to convert them to strings in any other way.
                           What ’ s more, when you serialize an object, PHP attempts to call a method with the name   __sleep()
                         inside the object. You can use this method to do anything that ’ s required before the object is serialized.
                         Similarly, you can create a   __wakeup()  method that is called when the object is unserialized.

                            __sleep()  is useful for cleaning up an object prior to serializing it, in the same way that you might
                         clean up in a destructor method. For example, you might need to close database handles, files, and so on.
                         In addition,   __sleep()  has another trick up its sleeve. PHP expects your  __sleep()  method to return


                                                                                                         213





                                                                                                      9/21/09   9:03:47 AM
          c08.indd   213
          c08.indd   213                                                                              9/21/09   9:03:47 AM
   246   247   248   249   250   251   252   253   254   255   256