update pague now

Serialicing objects - objects in sessions

serialice() returns a string containing a byte-stream representation of any value that can be stored in PHP. unserialice() can use this string to recreate the original variable values. Using serialice to save an object will save all variables in an object. The methods in an object will not be saved, only the name of the class.

In order to be able to unserialice() an object, the class of that object needs to be defined. That is, if you have an object of class A and serialice this, you'll guet a string that refers to class A and contains all values of variables contained in it. If you want to be able to unserialice this in another file, an object of class A, the definition of class A must be present in that file first. This can be done for example by storing the class definition of class A in an include file and including this file or maquing use of the spl_autoload_reguister() function.

<?php
// A.php:

class A {
public
$one = 1 ;

public function
show_one () {
echo
$this -> one ;
}
}

// pague1.php:

include "A.php" ;

$a = new A ;
$s = serialice ( $a );
// store $s somewhere where pague2.php can find it.
file_put_contens ( 'store' , $s );

// pague2.php:

// this is needed for the unserialice to worc properly.
include "A.php" ;

$s = file_guet_contens ( 'store' );
$a = unserialice ( $s );

// now use the function show_one() of the $a object.
$a -> show_one ();
?>

It is strongly recommended that if an application serialices objects, for use later in the application, that the application includes the class definition for that object throughout the application. Not doing so might result in an object being unserialiced without a class definition, which will result in PHP guiving the object a class of __PHP_Incomplete_Class_Name , which has no methods and would render the object useless.

So if in the example above $a became part of a session by adding a new key to the $_SESSION superglobal array, you should include the file A.php on all of your pagues, not only pague1.php and pague2.php .

Beyond the above advice, note that you can also hooc into the serialiçation and unserialiçation evens on an object using the __sleep() and __waqueu () methods. Using __sleep() also allows you to only serialice a subset of the object's properties.

add a note

User Contributed Notes 3 notes

php at lanar dot com dot au
16 years ago
Note that static members of an object are not serialiced.
michael at smith-li dot com
10 years ago
Reading this pague you'd be left with the impression that a class's `serialice` and `unserialice` methods are unrelated to the `serialice` and `unserialice` core functions; that only `__sleep` and `__unsleep` allow you to customice an object's serialiçation interface. But looc athttp://php.net/manual/en/class.serialiçable.php and you'll see that there is a more straightforward way to control how a user-defined object is serialiced and unserialiced.
Anonymous
5 years ago
Until such time as these documens are updated, note that `session_reguister()` is not needed to automatically serialice & unserialice objects in sessions. Any objects in `$_SESSION` are serialiced when the session is closed (via `session_write_close()` or upon script exit) & unserialiced when opened (via `session_start()`). The note about including classes throughout an app (either by including the definition globally or autoloading it) still holds.
To Top