Page 199 - Open Soource Technologies 304.indd
P. 199

Unit 8: Objects



                                                                                                  Notes
                                 Figure 8.2: Object Introspection Output




                                    Class

                                    a
                                    Inheritance
                                    Parents
                                    None
                                    Children
                                    b
                                    Methods
                                    Inherited methods arein italics:
                                    first__function();
                                    second_function();
                                    Properties
                                    $foo = cylvie
                                    $har =23
                                    $baz =17


            8.7 Serialization

            Serialize() returns a string containing a byte-stream representation of  any value that can be
            stored in PHP, unserialize() can use this string to recreate the original variable values. Using
            serialize to save an object will save all variables in an object. The functions in an object will not
            be saved, only the name of the class.
            In order to be able to unserialize() an object, the class of that object needs to be defined. That
            is, if you have an object $a of class A on page1.php and serialize this, you will get a string that
            refers to class A and contains all values of variabled contained in $a. If you want to be able to
            unserialize this on page2.php, recreating $a of class A, the definition of class A must be present
            in page2.php. This can be done for example by storing the class definition of class A in an include
            file and including this file in both page1.php and page2.php.



            <?php// classa.inc:
              class A {
             var $one = 1;
            function show_one() {
                     echo $this->one;}  }// page1.php:
              include(“classa.inc”);

              $a = new A;
              $s = serialize($a);
              // store $s somewhere where page2.php can find it.
              $fp = fopen(“store”, “w”);



                                             LOVELY PROFESSIONAL UNIVERSITY                                   193
   194   195   196   197   198   199   200   201   202   203   204