05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Here are some sample classes and objects that exercise the introspection functions<br />

from Example 6-2:<br />

class A {<br />

var $foo = 'foo';<br />

var $bar = 'bar';<br />

var $baz = 17.0;<br />

function first_function( ) { }<br />

function second_function( ) { }<br />

};<br />

class B extends A {<br />

var $quux = false;<br />

function third_function( ) { }<br />

};<br />

class C extends B {<br />

};<br />

$a = new A;<br />

$a->foo = 'sylvie';<br />

$a->bar = 23;<br />

$b = new B;<br />

$b->foo = 'bruno';<br />

$b->quux = true;<br />

$c = new C;<br />

print_object_info($a);<br />

print_object_info($b);<br />

print_object_info($c);<br />

Figure 6-2 shows the output of this code.<br />

Serialization<br />

Serializing an object means converting it to a bytestream representation that can be<br />

stored in a file. This is useful for persistent data; for example, <strong>PHP</strong> sessions automaticallysave<br />

and restore objects. Serialization in <strong>PHP</strong> is mostlyautomatic—it requires little<br />

extra work from you, beyond calling the serialize( ) and unserialize( ) functions:<br />

$encoded = serialize(something);<br />

$something = unserialize(encoded);<br />

Serialization is most commonlyused with <strong>PHP</strong>’s sessions, which handle the serialization<br />

for you. All you need to do is tell <strong>PHP</strong> which variables to keep track of, and<br />

they’re automatically preserved between visits to pages on your site. However, sessions<br />

are not the only use of serialization—if you want to implement your own form of<br />

persistent objects, the serialize( ) and unserialize( ) functions are a natural choice.<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.<br />

Serialization | 153

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!