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

MongoDB\BSON\Unserializable::bsonUnserialize · Constructs the object from a BSON array or document

· 52CE 我爱测

MongoDB\BSON\Unserializable::bsonUnserialize — Constructs the object from a BSON array or document

MongoDB\BSON\Unserializable::bsonUnserialize

(mongodb >=1.0.0)

MongoDB\BSON\Unserializable::bsonUnserialize — Constructs the object from a BSON array or document

说明

abstract public function MongoDB\BSON\Unserializable::bsonUnserialize(array $data): void

Called during unserialization of the object from BSON. The properties of the BSON array or document will be passed to the method as an array.

Remember to check for an _id property when handling data from a BSON document.

注意:

This method acts as the constructor of the object. The __construct() method will not be called after this method.

参数

data (array)

Properties within the BSON array or document.

返回值

The return value from this method is ignored.

更新日志

版本 说明
PECL mongodb 2.0.0 之前声明为暂定的返回类型现在已被强制执行。

示例

示例 #1 MongoDB\BSON\Unserializable::bsonUnserialize() example

<?php

class MyDocument implements MongoDB\BSON\Unserializable
{
    private $data = [];

    function bsonUnserialize(array $data): void
    {
        $this->data = $data;
    }
}

$bson = MongoDB\BSON\Document::fromJSON('{ "foo": "bar" }');

var_dump($bson->toPHP(['root' => 'MyDocument']));

?>

以上示例会输出:

object(MyDocument)#1 (1) {
  ["data":"MyDocument":private]=>
  array(1) {
    ["foo"]=>
    string(3) "bar"
  }
}

参见

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

资料来源与适用范围

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

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

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