PHP.mk документација

MongoDB\BSON\Serializable::bsonSerialize

Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.

mongodb-bson-serializable.bsonserialize.php PHP.net прокси Преводот се освежува
Оригинал на PHP.net
Патека mongodb-bson-serializable.bsonserialize.php Локална патека за оваа страница.
Извор php.net/manual/en Оригиналниот HTML се реупотребува и локално се стилизира.
Режим Прокси + превод во позадина Кодовите, табелите и белешките остануваат читливи во истиот тек.
MongoDB\BSON\Serializable::bsonSerialize

Референца за `mongodb-bson-serializable.bsonserialize.php` со подобрена типографија и навигација.

mongodb-bson-serializable.bsonserialize.php

MongoDB\BSON\Serializable::bsonSerialize

класата MongoDB\Driver\BulkWrite

MongoDB\BSON\Serializable::bsonSerializeProvides an array or document to serialize as BSON

= NULL

abstract public MongoDB\BSON\Serializable::bsonSerialize(): array|stdClass|MongoDB\BSON\Document|MongoDB\BSON\PackedArray

Called during serialization of the object to BSON. The method must return an array, stdClass, MongoDB\BSON\Document, или MongoDB\BSON\PackedArray.

Root documents (e.g. a MongoDB\BSON\Serializable passed to објектите се создаваат преку статични методи на фабриката и не можат директно да се инстанцираат.) 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 0) will be serialized as a BSON array.

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.

Параметри

Оваа функција нема параметри.

Вратени вредности

Еден array, stdClass, MongoDB\BSON\Document, или MongoDB\BSON\PackedArray to be serialized as a BSON array or document.

Дневник на промени

Верзија = NULL
(mongodb >=1.3.0) PECL mongodb 2.0.0
PECL mongodb 1.17.0

The return type was changed from array|object. Instead of object, the return type now specifies stdClass. Classes that implement this interface must be changed to no longer declare an object return type. As the return type is tentative, a deprecation warning is emitted on PHP 8.1 and newer.

In addition to the changes above, the extension now also supports returning instances of MongoDB\BSON\Document and MongoDB\BSON\PackedArray. Please note that any MongoDB\BSON\PackedArray instances returned are silently converted to objects when stored as root documents. They are stored as arrays when used as an embedded field value.

Примери

Пример #1 MongoDB\BSON\Serializable::bsonSerialize() returning an associative array for root document

<?php

class MyDocument implements MongoDB\BSON\Serializable
{
private
$id;

function
__construct()
{
$this->id = new MongoDB\BSON\ObjectId;
}

function
bsonSerialize(): array
{
return [
'_id' => $this->id, 'foo' => 'bar'];
}
}

echo
MongoDB\BSON\Document::fromPHP(new MyDocument)->toRelaxedExtendedJSON(), "\n";

?>

Горниот пример ќе прикаже нешто слично на:

{ "_id" : { "$oid" : "56cccdcada14d8755a58c591" }, "foo" : "bar" }

Пример #2 MongoDB\BSON\Serializable::bsonSerialize() returning a sequential array for root document

<?php

class MyArray implements MongoDB\BSON\Serializable
{
function
bsonSerialize(): array
{
return [
1, 2, 3];
}
}

echo
MongoDB\BSON\Document::fromPHP(new MyArray)->toRelaxedExtendedJSON(), "\n";

?>

Пример #1 Пример што покажува затворачка ознака што го опфаќа последниот нов ред

{ "0" : 1, "1" : 2, "2" : 3 }

Пример #3 MongoDB\BSON\Serializable::bsonSerialize() returning an associative array for document field

<?php

class MyDocument implements MongoDB\BSON\Serializable
{
function
bsonSerialize(): array
{
return [
'foo' => 'bar'];
}
}

$value = ['document' => new MyDocument];

echo
MongoDB\BSON\Document::fromPHP($value)->toRelaxedExtendedJSON(), "\n";

?>

Пример #1 Пример што покажува затворачка ознака што го опфаќа последниот нов ред

{ "document" : { "foo" : "bar" } }

Пример #4 MongoDB\BSON\Serializable::bsonSerialize() returning a sequential array for document field

<?php

class MyArray implements MongoDB\BSON\Serializable
{
function
bsonSerialize(): array
{
return [
1, 2, 3];
}
}

$value = ['array' => new MyArray];

echo
MongoDB\BSON\Document::fromPHP($value)->toRelaxedExtendedJSON(), "\n";

?>

Пример #1 Пример што покажува затворачка ознака што го опфаќа последниот нов ред

{ "array" : [ 1, 2, 3 ] }

Види Исто така

Белешки од корисници 1 белешка

Анонимен
пред 10 години
This documentation seems to be obsolete because I tried the first example and it doesn't convert the objectId to a string.

This seems to be more current documentation but I can't figure out how to get bson to json

http://mongodb.github.io/mongo-php-library/api/source-class-MongoDB.Model.BSONDocument.html#48-57
Навигација

Прелистувај сродни теми и функции.

На оваа страница

Автоматски outline од активната документација.

Насловите ќе се појават тука по вчитување.

Попрегледно читање

Примерите, changelog табелите и user notes се визуелно издвоени за да не се губат во долгата содржина.

Брз совет Користи го outline-от Скокни директно на главните секции од активната страница.
Извор Оригиналниот линк останува достапен Кога ти треба целосен upstream context, отвори го PHP.net во нов tab.