PHP.nl

serialize

serialize

Generates a storable representation of a value

string **serialize** mixed $value

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

value The value to be serialized. handles all types, except the -type and some s (see note below). You can even arrays that contain references to itself. Circular references inside the array/object you are serializing will also be stored. Any other reference will be lost. serialize``resource``object``serialize

   When serializing objects, PHP will attempt to call the member functions
    or
    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 
    the  or
    member function is called.
  __serialize()__sleep()`unserialize`__unserialize()__wakeup()

Opmerking: > 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.

Returns a string containing a byte-stream representation of that can be stored anywhere. value

Note that this is a binary string which may include null bytes, and needs to be stored and handled as such. For example, output should generally be stored in a BLOB field in a database, rather than a CHAR or TEXT field. serialize

Voorbeeld: 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, array_reverse($sqldata))) {
        /* Something went wrong.. */
    }
}
?>

Opmerking: > Note that many built-in PHP objects cannot be serialized. However, those with this ability either implement the interface or the magic / or / methods. If an internal class does not fulfill any of those requirements, it cannot reliably be serialized. Serializable__serialize()__unserialize()__sleep()__wakeup()

There are some historical exceptions to the above rule, where some internal objects could be serialized without implementing the interface or exposing the methods.

Waarschuwing: > When serializes objects, the leading backslash is not included in the class name of namespaced classes for maximum compatibility. serialize

unserialize``var_export``json_encodeSerializing Objects__sleep()__wakeup()__serialize()__unserialize()