大网数据广告活动
PHP 语言与运行手册

igbinary_serialize · Generates a compact, storable binary representation of a value

· 52CE 我爱测

igbinary_serialize — Generates a compact, storable binary representation of a value

igbinary_serialize

(PECL igbinary >= 1.1.1)

igbinary_serialize — Generates a compact, storable binary representation of a value

说明

function igbinary_serialize(mixed $value): string|false

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, igbinary_unserialize() can be used.

参数

value

The value to be serialized. igbinary_serialize() handles all types, except the resource-type and some objects (see note below). Even arrays that contain references to itself can be processed by igbinary_serialize(). Circular references inside the array or object that is being serializend will also be stored. Any other reference will be lost.

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

注意:

Private members of objects 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 value that can be stored anywhere.

Note that this is a binary string which can include any byte value, and needs to be stored and handled as such. For example, igbinary_serialize() output should generally be stored in a BLOB field in a database, rather than a CHAR or TEXT field.

示例

示例 #1 igbinary_serialize() example

<?php
$ser = igbinary_serialize(['test', 'test']);
echo urlencode($ser), "\n";
var_export(igbinary_unserialize($ser));
?>

以上示例会输出:

%00%00%00%02%14%02%06%00%11%04test%06%01%0E%00
array (
  0 => 'test',
  1 => 'test',
)

注释

注意:

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

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

参见

来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
PHP Documentation Group
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。