Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

serialize(3) [php man page]

SERIALIZE(3)								 1							      SERIALIZE(3)

serialize - Generates a storable representation of a value

SYNOPSIS
string serialize (mixed $value) DESCRIPTION
Generates a storable representation of a value. This is useful for storing or passing PHP values around without losing their type and structure. To make the serialized string into a PHP value again, use unserialize(3). PARAMETERS
o $value - The value to be serialized. serialize(3) handles all types, except the resource-type. You can even serialize(3) arrays that con- tain references to itself. Circular references inside the array/object you are serializing will also be stored. Any other refer- ence will be lost. When serializing objects, PHP will attempt to call the member function __sleep() prior to serialization. This is to allow the object to do any last minute clean-up, etc. prior to being serialized. Likewise, when the object is restored using unserialize(3) the __wakeup() member function is called. Note Object's private members have the class name prepended to the member name; protected members have a '*' prepended to the member name. These prepended values have null bytes on either side. RETURN VALUES
Returns a string containing a byte-stream representation of $value that can be stored anywhere. Note that this is a binary string which may include null bytes, and needs to be stored and handled as such. For example, serialize(3) out- put should generally be stored in a BLOB field in a database, rather than a CHAR or TEXT field. EXAMPLES
Example #1 serialize(3) example <?php // $session_data contains a multi-dimensional array with session // information for the current user. We use serialize() to store // it in a database at the end of the request. $conn = odbc_connect("webdb", "php", "chicken"); $stmt = odbc_prepare($conn, "UPDATE sessions SET data = ? WHERE id = ?"); $sqldata = array (serialize($session_data), $_SERVER['PHP_AUTH_USER']); if (!odbc_execute($stmt, $sqldata)) { $stmt = odbc_prepare($conn, "INSERT INTO sessions (id, data) VALUES(?, ?)"); if (!odbc_execute($stmt, $sqldata)) { /* Something went wrong.. */ } } ?> NOTES
Note Note that many built-in PHP objects cannot be serialized. However, those with this ability either implement the Serializable inter- face or the magic __sleep() and __wakeup() methods. If an internal class does not fulfill any of those requirements, it cannot reli- ably be serialized. There are some historical exceptions to the above rule, where some internal objects could be serialized without implementing the interface or exposing the methods. Notably, the ArrayObject prior to PHP 5.2.0. Warning When serialize(3) serializes objects, the leading backslash is not included in the class name of namespaced classes for maximum compatibility. SEE ALSO
unserialize(3), var_export(3), json_encode(3), Serializing Objects, __sleep(), __wakeup(). PHP Documentation Group SERIALIZE(3)

Check Out this Related Man Page

SPLOBJECTSTORAGE(3)							 1						       SPLOBJECTSTORAGE(3)

The SplObjectStorage class

INTRODUCTION
The SplObjectStorage class provides a map from objects to data or, by ignoring data, an object set. This dual purpose can be useful in many cases involving the need to uniquely identify objects. CLASS SYNOPSIS
SplObjectStorage SplObjectStorageCountableIteratorSerializableArrayAccess Methods o public void SplObjectStorage::addAll (SplObjectStorage $storage) o public void SplObjectStorage::attach NULL (object $object, [mixed $data]) o public bool SplObjectStorage::contains (object $object) o public int SplObjectStorage::count (void ) o public object SplObjectStorage::current (void ) o public void SplObjectStorage::detach (object $object) o public string SplObjectStorage::getHash (object $object) o public mixed SplObjectStorage::getInfo (void ) o public int SplObjectStorage::key (void ) o public void SplObjectStorage::next (void ) o public bool SplObjectStorage::offsetExists (object $object) o public mixed SplObjectStorage::offsetGet (object $object) o public void SplObjectStorage::offsetSet NULL (object $object, [mixed $data]) o public void SplObjectStorage::offsetUnset (object $object) o public void SplObjectStorage::removeAll (SplObjectStorage $storage) o public void SplObjectStorage::removeAllExcept (SplObjectStorage $storage) o public void SplObjectStorage::rewind (void ) o public string SplObjectStorage::serialize (void ) o public void SplObjectStorage::setInfo (mixed $data) o public void SplObjectStorage::unserialize (string $serialized) o public bool SplObjectStorage::valid (void ) EXAMPLES
Example #1 SplObjectStorage as a set <?php // As an object set $s = new SplObjectStorage(); $o1 = new StdClass; $o2 = new StdClass; $o3 = new StdClass; $s->attach($o1); $s->attach($o2); var_dump($s->contains($o1)); var_dump($s->contains($o2)); var_dump($s->contains($o3)); $s->detach($o2); var_dump($s->contains($o1)); var_dump($s->contains($o2)); var_dump($s->contains($o3)); ?> The above example will output: bool(true) bool(true) bool(false) bool(true) bool(false) bool(false) Example #2 SplObjectStorage as a map <?php // As a map from objects to data $s = new SplObjectStorage(); $o1 = new StdClass; $o2 = new StdClass; $o3 = new StdClass; $s[$o1] = "data for object 1"; $s[$o2] = array(1,2,3); if (isset($s[$o2])) { var_dump($s[$o2]); } ?> The above example will output: array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } PHP Documentation Group SPLOBJECTSTORAGE(3)
Man Page