array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'zh', ), 'this' => array ( 0 => 'mongodb-bson-unserializable.bsonunserialize.php', 1 => 'MongoDB\\BSON\\Unserializable::bsonUnserialize', 2 => 'Constructs the object from a BSON array or document', ), 'up' => array ( 0 => 'class.mongodb-bson-unserializable.php', 1 => 'MongoDB\\BSON\\Unserializable', ), 'prev' => array ( 0 => 'class.mongodb-bson-unserializable.php', 1 => 'MongoDB\\BSON\\Unserializable', ), 'next' => array ( 0 => 'class.mongodb-bson-binaryinterface.php', 1 => 'MongoDB\\BSON\\BinaryInterface', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/mongodb/bson/unserializable/bsonunserialize.xml', ), 'history' => array ( ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); contributors($setup); ?>

MongoDB\BSON\Unserializable::bsonUnserialize

(mongodb >=1.0.0)

MongoDB\BSON\Unserializable::bsonUnserializeConstructs the object from a BSON array or document

说明

abstract public 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"
  }
}

参见