MongoDB\BSON\Serializable::bsonSerializeProvides an array or document to serialize as BSON 説明
abstract public arraystdClassMongoDB\BSON\DocumentMongoDB\BSON\PackedArray MongoDB\BSON\Serializable::bsonSerialize()
Called during serialization of the object to BSON. The method must return an array, stdClass, MongoDB\BSON\Document, or MongoDB\BSON\PackedArray.
Root documents (e.g. a
MongoDB\BSON\Serializable passed to
MongoDB\BSON\Document::fromPHP) will always be
serialized as a BSON document. For field values, associative arrays and
stdClass instances will be serialized as a BSON
document and sequential arrays (i.e. sequential, numeric indexes starting at
Users are encouraged to include an _id property (e.g. a MongoDB\BSON\ObjectId initialized in the constructor) when returning data for a BSON root document. In the absence of an _id property, the extension or server will generate a MongoDB\BSON\ObjectId for insert or upsert operations, respectively. パラメータこの関数にはパラメータはありません。 戻り値An array, stdClass, MongoDB\BSON\Document, or MongoDB\BSON\PackedArray to be serialized as a BSON array or document. 変更履歴
例例1 MongoDB\BSON\Serializable::bsonSerialize returning an associative array for root document
上の例の出力は、 たとえば以下のようになります。 { "_id" : { "$oid" : "56cccdcada14d8755a58c591" }, "foo" : "bar" } 例2 MongoDB\BSON\Serializable::bsonSerialize returning a sequential array for root document
上の例の出力は以下となります。 { "0" : 1, "1" : 2, "2" : 3 } 例3 MongoDB\BSON\Serializable::bsonSerialize returning an associative array for document field
上の例の出力は以下となります。 { "document" : { "foo" : "bar" } } 例4 MongoDB\BSON\Serializable::bsonSerialize returning a sequential array for document field
上の例の出力は以下となります。 { "array" : [ 1, 2, 3 ] } 参考
|