(PHP 4, PHP 5)
unserialize — 从已存储的表示中创建 PHP 的值
unserialize() 对单一的已序列化的变量进行操作,将其转换回
PHP 的值。返回的是转换之后的值,可为
integer、float、string、array
或 object。如果传递的字符串不可解序列化,则返回 FALSE
。
Note: unserialize_callback_func 指令
如果在解序列化的时候需要实例化一个未定义类,则可以设置回调函数以供调用(以免得到的是不完整的 object “__PHP_Incomplete_Class”)。可通过 php.ini、ini_set() 或 .htaccess 定义‘unserialize_callback_func’。每次实例化一个未定义类时它都会被调用。若要禁止这个特性,只需置空此设定。还需要注意的是 unserialize_callback_func 指令是从 PHP 4.2.0 开始提供使用的。
Note:
callback
参数是在 PHP 4.2.0 中添加的
若被解序列化的变量是一个对象,在成功地重新构造对象之后,PHP 会自动地试图去调用 __wakeup() 成员函数(如果存在的话)。
Example #1 unserialize_callback_func 示例
<?php
$serialized_object='O:1:"a":1:{s:5:"value";s:3:"100";}';
// unserialize_callback_func 从 PHP 4.2.0 起可用
ini_set('unserialize_callback_func','mycallback'); // 设置您的回调函数
function mycallback($classname) {
// 只需包含含有类定义的文件
// $classname 指出需要的是哪一个类
}
?>
Note:
在 PHP 3 中,解序列化一个对象时是不保存方法的。而 PHP 4 打破了这个限制,同时保存了属性和方法。请参见类与对象中的序列化对象部分获取更多信息。
Example #2 unserialize() 示例
<?php
// 这里,我们使用 unserialize() 装载来自数据库的 $session_data 数组中的会话数据。
// 此例是描述 serialize() 的那个例子的补充。
$conn = odbc_connect ("webdb", "php", "chicken");
$stmt = odbc_prepare ($conn, "SELECT data FROM sessions WHERE id = ?");
$sqldata = array ($PHP_AUTH_USER);
if (!odbc_execute ($stmt, &$sqldata) || !odbc_fetch_into ($stmt, &$tmp)) {
// 如果执行出错或返回错误,则初始化为空数组
$session_data = array();
} else {
// 现在我们需要的是 $tmp[0] 中已序列化的数据。
$session_data = unserialize ($tmp[0]);
if (!is_array ($session_data)) {
// 出错,初始化为空数组
$session_data = array();
}
}
?>
参见 serialize()。
str
The serialized string.
If the variable being unserialized is an object, after successfully reconstructing the object PHP will automatically attempt to call the __wakeup() member function (if it exists).
Note: unserialize_callback_func directive
It's possible to set a callback-function which will be called, if an undefined class should be instantiated during unserializing. (to prevent getting an incomplete object "__PHP_Incomplete_Class".) Use your php.ini, ini_set() or .htaccess to define 'unserialize_callback_func'. Everytime an undefined class should be instantiated, it'll be called. To disable this feature just empty this setting.
The converted value is returned, and can be a boolean, integer, float, string, array or object.
In case the passed string is not unserializeable, FALSE
is returned and
E_NOTICE
is issued.
版本 | 说明 |
---|---|
4.2.0 | The directive unserialize_callback_func became available. |
Example #3 unserialize() example
<?php
// Here, we use unserialize() to load session data to the
// $session_data array from the string selected from a database.
// This example complements the one described with serialize().
$conn = odbc_connect("webdb", "php", "chicken");
$stmt = odbc_prepare($conn, "SELECT data FROM sessions WHERE id = ?");
$sqldata = array($_SERVER['PHP_AUTH_USER']);
if (!odbc_execute($stmt, $sqldata) || !odbc_fetch_into($stmt, $tmp)) {
// if the execute or fetch fails, initialize to empty array
$session_data = array();
} else {
// we should now have the serialized data in $tmp[0].
$session_data = unserialize($tmp[0]);
if (!is_array($session_data)) {
// something went wrong, initialize to empty array
$session_data = array();
}
}
?>
Example #4 unserialize_callback_func example
<?php
$serialized_object='O:1:"a":1:{s:5:"value";s:3:"100";}';
// unserialize_callback_func directive available as of PHP 4.2.0
ini_set('unserialize_callback_func', 'mycallback'); // set your callback_function
function mycallback($classname)
{
// just include a file containing your classdefinition
// you get $classname to figure out which classdefinition is required
}
?>
FALSE
is returned both in the case of an error and if unserializing
the serialized FALSE
value. It is possible to catch this special case by
comparing str
with
serialize(false) or by catching the issued
E_NOTICE
.