home support FAQ resources services partners contact us contact us
 Web Application Previous  Next  
 Tutorial

Serializer

In PHP 3.0, you couldn’t serialize objects easily.The serialize() function didn’t
preserve class methods properly, and there was no way to do it manually.The PHP 3.0
support for objects was missing one important thing: introspection.There was no way
to get the name of a class or the name of its parent class.Therefore, the PHPLib
needed to use a workaround: It simply required classes to have two additional values,
$classname and $persistent_slots, which contained the name of the class and the
class variables to be serialized, respectively. Knowing the class name, the PHPLib could
create PHP code instantiating the class ($class = new class;) and store it in the
session data repository.When the session data was reactivated, this code was executed
with eval(). Do you remember the self-modifying counter example from Chapter 2,
“Advanced Syntax”? The PHPLib uses the same concepts.
Note:With PHP 4.0, these workarounds are no longer necessary. PHP 4.0 has
functions like get_class() and get_parent_class() to allow better class introspection.
And serialize() now works transparently on objects.
Session Example

In your daily work, using the PHPLib session object is just as easy as using the PHP
4.0 session library.The example in Listing 6.3 reflects this; it does the same work as
the example in Chapter 4:
Listing 6.3 A basic example of using PHPLib’s Session class.
// Create a new instance manually
$sess = new Example_Session;
// Start the session
$sess->start();
// Register our session variable
$sess->register(“counter”);
// Init the counter
if(!isset($counter))
{
$counter = 0;
}
// Output session ID and counter
printf(“Our session ID is: %s<br>”, $sess->id);
print(“The counter value is: $counter”);
// Increment the counter
$counter++;
// Save the session state
$sess->freeze();
Previous  Next  
Link Partners: Asia florist, Flowers to India, Hong kong flowers, Site submit, Cheap web hosting, China florist, Japan florist