<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/mongodb.architecture.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'uk',
  ),
  'this' => 
  array (
    0 => 'mongodb.persistence.php',
    1 => 'Persisting Data',
    2 => 'Serialization and deserialization of PHP variables into MongoDB',
  ),
  'up' => 
  array (
    0 => 'mongodb.architecture.php',
    1 => 'Driver Architecture and Internals',
  ),
  'prev' => 
  array (
    0 => 'mongodb.connection-handling.php',
    1 => 'Connections',
  ),
  'next' => 
  array (
    0 => 'mongodb.security.php',
    1 => 'Security',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/mongodb/architecture.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="mongodb.persistence" class="section">
  
  <h2 class="title">Serialization and deserialization of PHP variables into MongoDB</h2>

  <p class="para">
   This document discusses how compound structures (i.e. documents, arrays, and
   objects) are converted between BSON and PHP values.
  </p>

  <div class="section" id="mongodb.persistence.serialization">
   <h2 class="title">Serialization to BSON</h2>

   <div class="section">
    <h2 class="title">Arrays</h2>

    <p class="para">
     If an array is a <em>packed array</em> — i.e. empty array or
     the keys start at 0 and are sequential without gaps: <em>BSON
     array</em>.
    </p>

    <p class="para">
     If the array is not packed — i.e. having associative (string) keys, the
     keys don&#039;t start at 0, or when there are gaps:: <em>BSON
     object</em>
    </p>

    <p class="para">
     A top-level (root) document, <em>always</em> serializes as a
     BSON document.
    </p>

    <div class="section">
     <h2 class="title">Examples</h2>

     <p class="para">
      These serialize as a BSON array:
     </p>

     <div class="example-contents">
<div class="textcode"><pre class="textcode">[ 8, 5, 2, 3 ] =&gt; [ 8, 5, 2, 3 ]
[ 0 =&gt; 4, 1 =&gt; 9 ] =&gt; [ 4, 9 ]</pre>
</div>
     </div>


     <p class="para">
      These serialize as a BSON document:
     </p>

     <div class="example-contents">
<div class="textcode"><pre class="textcode">[ 0 =&gt; 1, 2 =&gt; 8, 3 =&gt; 12 ] =&gt; { &quot;0&quot; : 1, &quot;2&quot; : 8, &quot;3&quot; : 12 }
[ &quot;foo&quot; =&gt; 42 ] =&gt; { &quot;foo&quot; : 42 }
[ 1 =&gt; 9, 0 =&gt; 10 ] =&gt; { &quot;1&quot; : 9, &quot;0&quot; : 10 }</pre>
</div>
     </div>


     <p class="para">
      Note that the five examples are <em>extracts</em> of a full
      document, and represent only <em>one</em> value inside a
      document.
     </p>

    </div>
   </div>

   <div class="section">
    <h2 class="title">Objects</h2>

     <p class="para">
      If an object is of the <span class="classname"><a href="class.stdclass.php" class="classname">stdClass</a></span> class, serialize
      as a <em>BSON document</em>.
     </p>

     <p class="para">
      If an object is a supported class that implements
      <span class="interfacename"><a href="class.mongodb-bson-type.php" class="interfacename">MongoDB\BSON\Type</a></span>, then use the BSON
      serialization logic for that specific type.
      <span class="interfacename"><a href="class.mongodb-bson-type.php" class="interfacename">MongoDB\BSON\Type</a></span> instances (excluding
      <span class="interfacename"><a href="class.mongodb-bson-serializable.php" class="interfacename">MongoDB\BSON\Serializable</a></span> may only be
      serialized as a document field value. Attempting to serialize such an
      object as a root document will throw a
      <span class="classname"><a href="class.mongodb-driver-exception-unexpectedvalueexception.php" class="classname">MongoDB\Driver\Exception\UnexpectedValueException</a></span>
     </p>

     <p class="para">
      If an object is of an unknown class implementing the
      <span class="interfacename"><a href="class.mongodb-bson-type.php" class="interfacename">MongoDB\BSON\Type</a></span> interface, then throw a
      <span class="classname"><a href="class.mongodb-driver-exception-unexpectedvalueexception.php" class="classname">MongoDB\Driver\Exception\UnexpectedValueException</a></span>
     </p>

     <p class="para">
      If an object is of any other class, without implementing any special
      interface, serialize as a <em>BSON document</em>. Keep only
      <em>public</em> properties, and ignore
      <em>protected</em> and <em>private</em>
      properties.
     </p>

     <p class="para">
      If an object is of a class that implements the
      <span class="interfacename"><a href="class.mongodb-bson-serializable.php" class="interfacename">MongoDB\BSON\Serializable</a></span> interface, call
      <span class="methodname"><a href="mongodb-bson-serializable.bsonserialize.php" class="methodname">MongoDB\BSON\Serializable::bsonSerialize()</a></span> and use
      the returned array or <span class="classname"><a href="class.stdclass.php" class="classname">stdClass</a></span> to serialize as a
      BSON document or array. The BSON type will be determined by the following:
     </p>

     <p class="para">
      <ol type="1">
       <li class="listitem">
        <p class="para">Root documents must be serialized as a BSON
        document.
        </p>
       </li>
       <li class="listitem">
        <p class="para">
         <span class="interfacename"><a href="class.mongodb-bson-persistable.php" class="interfacename">MongoDB\BSON\Persistable</a></span> objects must be
         serialized as a BSON document.
        </p>
       </li>
       <li class="listitem">
        <p class="para">
         If <span class="methodname"><a href="mongodb-bson-serializable.bsonserialize.php" class="methodname">MongoDB\BSON\Serializable::bsonSerialize()</a></span>
         returns a packed array, serialize as a BSON array.
        </p>
       </li>
       <li class="listitem">
        <p class="para">
         If <span class="methodname"><a href="mongodb-bson-serializable.bsonserialize.php" class="methodname">MongoDB\BSON\Serializable::bsonSerialize()</a></span>
         returns a non-packed array or <span class="classname"><a href="class.stdclass.php" class="classname">stdClass</a></span>,
         serialize as a BSON document.
        </p>
       </li>
       <li class="listitem">
        <p class="para">
         If <span class="methodname"><a href="mongodb-bson-serializable.bsonserialize.php" class="methodname">MongoDB\BSON\Serializable::bsonSerialize()</a></span>
         did not return an array or <span class="classname"><a href="class.stdclass.php" class="classname">stdClass</a></span>, throw an
         <span class="classname"><a href="class.mongodb-driver-exception-unexpectedvalueexception.php" class="classname">MongoDB\Driver\Exception\UnexpectedValueException</a></span>
         exception.
        </p>
       </li>
      </ol>
     </p>

     <p class="para">
      If an object is of a class that implements the
      <span class="interfacename"><a href="class.mongodb-bson-persistable.php" class="interfacename">MongoDB\BSON\Persistable</a></span> interface (which
      implies <span class="interfacename"><a href="class.mongodb-bson-serializable.php" class="interfacename">MongoDB\BSON\Serializable</a></span>), obtain
      the properties in a similar way as in the previous paragraphs, but
      <em>also</em> add an additional property
      <span class="property">__pclass</span> as a Binary value, with subtype
      <code class="literal">0x80</code> and data bearing the fully qualified class name
      of the object that is being serialized.
     </p>

     <p class="para">
      The <span class="property">__pclass</span> property is added to the array or
      object returned by
      <span class="methodname"><a href="mongodb-bson-serializable.bsonserialize.php" class="methodname">MongoDB\BSON\Serializable::bsonSerialize()</a></span>, which
      means it will overwrite any <span class="property">__pclass</span> key/property in
      the <span class="methodname"><a href="mongodb-bson-serializable.bsonserialize.php" class="methodname">MongoDB\BSON\Serializable::bsonSerialize()</a></span>
      return value. If you want to avoid this behaviour and set your own
      <span class="property">__pclass</span> value, you must <em>not</em>
      implement <span class="interfacename"><a href="class.mongodb-bson-persistable.php" class="interfacename">MongoDB\BSON\Persistable</a></span> and
      should instead implement
      <span class="interfacename"><a href="class.mongodb-bson-serializable.php" class="interfacename">MongoDB\BSON\Serializable</a></span> directly.
     </p>

     <div class="section">
      <h2 class="title">Examples</h2>

      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">stdClass<br /></span><span style="color: #007700">{<br />    public </span><span style="color: #0000BB">$foo </span><span style="color: #007700">= </span><span style="color: #0000BB">42</span><span style="color: #007700">;<br />} </span><span style="color: #FF8000">// =&gt; {"foo": 42}<br /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">MyClass<br /></span><span style="color: #007700">{<br />    public </span><span style="color: #0000BB">$foo </span><span style="color: #007700">= </span><span style="color: #0000BB">42</span><span style="color: #007700">;<br />    protected </span><span style="color: #0000BB">$prot </span><span style="color: #007700">= </span><span style="color: #DD0000">'wine'</span><span style="color: #007700">;<br />    private </span><span style="color: #0000BB">$fpr </span><span style="color: #007700">= </span><span style="color: #DD0000">'cheese'</span><span style="color: #007700">;<br />} </span><span style="color: #FF8000">// =&gt; {"foo": 42}<br /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">AnotherClass1 </span><span style="color: #007700">implements </span><span style="color: #0000BB">MongoDB\BSON\Serializable<br /></span><span style="color: #007700">{<br />    public </span><span style="color: #0000BB">$foo </span><span style="color: #007700">= </span><span style="color: #0000BB">42</span><span style="color: #007700">;<br />    protected </span><span style="color: #0000BB">$prot </span><span style="color: #007700">= </span><span style="color: #DD0000">'wine'</span><span style="color: #007700">;<br />    private </span><span style="color: #0000BB">$fpr </span><span style="color: #007700">= </span><span style="color: #DD0000">'cheese'</span><span style="color: #007700">;<br /><br />    public function </span><span style="color: #0000BB">bsonSerialize</span><span style="color: #007700">(): array<br />    {<br />        return [</span><span style="color: #DD0000">'foo' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">foo</span><span style="color: #007700">, </span><span style="color: #DD0000">'prot' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prot</span><span style="color: #007700">];<br />    }<br />} </span><span style="color: #FF8000">// =&gt; {"foo": 42, "prot": "wine"}<br /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">AnotherClass2 </span><span style="color: #007700">implements </span><span style="color: #0000BB">MongoDB\BSON\Serializable<br /></span><span style="color: #007700">{<br />    public </span><span style="color: #0000BB">$foo </span><span style="color: #007700">= </span><span style="color: #0000BB">42</span><span style="color: #007700">;<br /><br />    public function </span><span style="color: #0000BB">bsonSerialize</span><span style="color: #007700">(): </span><span style="color: #0000BB">self<br />    </span><span style="color: #007700">{<br />        return </span><span style="color: #0000BB">$this</span><span style="color: #007700">;<br />    }<br />} </span><span style="color: #FF8000">// =&gt; MongoDB\Driver\Exception\UnexpectedValueException("bsonSerialize() did not return an array or stdClass")<br /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">AnotherClass3 </span><span style="color: #007700">implements </span><span style="color: #0000BB">MongoDB\BSON\Serializable<br /></span><span style="color: #007700">{<br />    private </span><span style="color: #0000BB">$elements </span><span style="color: #007700">= [</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">, </span><span style="color: #DD0000">'bar'</span><span style="color: #007700">];<br /><br />    public function </span><span style="color: #0000BB">bsonSerialize</span><span style="color: #007700">(): array<br />    {<br />        return </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">elements</span><span style="color: #007700">;<br />    }<br />} </span><span style="color: #FF8000">// =&gt; {"0": "foo", "1": "bar"}<br /><br />/**<br /> * Nesting Serializable classes<br /> */<br /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">AnotherClass4 </span><span style="color: #007700">implements </span><span style="color: #0000BB">MongoDB\BSON\Serializable<br /></span><span style="color: #007700">{<br />    private </span><span style="color: #0000BB">$elements </span><span style="color: #007700">= [</span><span style="color: #0000BB">0 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'foo'</span><span style="color: #007700">, </span><span style="color: #0000BB">2 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'bar'</span><span style="color: #007700">];<br /><br />    public function </span><span style="color: #0000BB">bsonSerialize</span><span style="color: #007700">(): array<br />    {<br />        return </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">elements</span><span style="color: #007700">;<br />    }<br />} </span><span style="color: #FF8000">// =&gt; {"0": "foo", "2": "bar"}<br /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">ContainerClass1 </span><span style="color: #007700">implements </span><span style="color: #0000BB">MongoDB\BSON\Serializable<br /></span><span style="color: #007700">{<br />    public </span><span style="color: #0000BB">$things</span><span style="color: #007700">;<br /><br />    public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">()<br />    {<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">things </span><span style="color: #007700">= new </span><span style="color: #0000BB">AnotherClass4</span><span style="color: #007700">();<br />    }<br /><br />    function </span><span style="color: #0000BB">bsonSerialize</span><span style="color: #007700">(): array<br />    {<br />        return [</span><span style="color: #DD0000">'things' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">things</span><span style="color: #007700">];<br />    }<br />} </span><span style="color: #FF8000">// =&gt; {"things": {"0": "foo", "2": "bar"}}<br /><br /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">AnotherClass5 </span><span style="color: #007700">implements </span><span style="color: #0000BB">MongoDB\BSON\Serializable<br /></span><span style="color: #007700">{<br />    private </span><span style="color: #0000BB">$elements </span><span style="color: #007700">= [</span><span style="color: #0000BB">0 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'foo'</span><span style="color: #007700">, </span><span style="color: #0000BB">2 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'bar'</span><span style="color: #007700">];<br /><br />    public function </span><span style="color: #0000BB">bsonSerialize</span><span style="color: #007700">(): array<br />    {<br />        return </span><span style="color: #0000BB">array_values</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">elements</span><span style="color: #007700">);<br />    }<br />} </span><span style="color: #FF8000">// =&gt; {"0": "foo", "1": "bar"} as a root class<br />  //    ["foo", "bar"] as a nested value<br /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">ContainerClass2 </span><span style="color: #007700">implements </span><span style="color: #0000BB">MongoDB\BSON\Serializable<br /></span><span style="color: #007700">{<br />    public </span><span style="color: #0000BB">$things</span><span style="color: #007700">;<br /><br />    public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">()<br />    {<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">things </span><span style="color: #007700">= new </span><span style="color: #0000BB">AnotherClass5</span><span style="color: #007700">();<br />    }<br /><br />    public function </span><span style="color: #0000BB">bsonSerialize</span><span style="color: #007700">(): array<br />    {<br />        return [</span><span style="color: #DD0000">'things' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">things</span><span style="color: #007700">];<br />    }<br />} </span><span style="color: #FF8000">// =&gt; {"things": ["foo", "bar"]}<br /><br /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">AnotherClass6 </span><span style="color: #007700">implements </span><span style="color: #0000BB">MongoDB\BSON\Serializable<br /></span><span style="color: #007700">{<br />    private </span><span style="color: #0000BB">$elements </span><span style="color: #007700">= [</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">, </span><span style="color: #DD0000">'bar'</span><span style="color: #007700">];<br /><br />    function </span><span style="color: #0000BB">bsonSerialize</span><span style="color: #007700">(): </span><span style="color: #0000BB">object<br />    </span><span style="color: #007700">{<br />        return (object) </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">elements</span><span style="color: #007700">;<br />    }<br />} </span><span style="color: #FF8000">// =&gt; {"0": "foo", "1": "bar"}<br /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">ContainerClass3 </span><span style="color: #007700">implements </span><span style="color: #0000BB">MongoDB\BSON\Serializable<br /></span><span style="color: #007700">{<br />    public </span><span style="color: #0000BB">$things</span><span style="color: #007700">;<br /><br />    public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">()<br />    {<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">things </span><span style="color: #007700">= new </span><span style="color: #0000BB">AnotherClass6</span><span style="color: #007700">();<br />    }<br /><br />    public function </span><span style="color: #0000BB">bsonSerialize</span><span style="color: #007700">(): array<br />    {<br />        return [</span><span style="color: #DD0000">'things' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">things</span><span style="color: #007700">];<br />    }<br />} </span><span style="color: #FF8000">// =&gt; {"things": {"0": "foo", "1": "bar"}}<br /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">UpperClass </span><span style="color: #007700">implements </span><span style="color: #0000BB">MongoDB\BSON\Persistable<br /></span><span style="color: #007700">{<br />    public </span><span style="color: #0000BB">$foo </span><span style="color: #007700">= </span><span style="color: #0000BB">42</span><span style="color: #007700">;<br />    protected </span><span style="color: #0000BB">$prot </span><span style="color: #007700">= </span><span style="color: #DD0000">'wine'</span><span style="color: #007700">;<br />    private </span><span style="color: #0000BB">$fpr </span><span style="color: #007700">= </span><span style="color: #DD0000">'cheese'</span><span style="color: #007700">;<br /><br />    private </span><span style="color: #0000BB">$data</span><span style="color: #007700">;<br /><br />    public function </span><span style="color: #0000BB">bsonUnserialize</span><span style="color: #007700">(array </span><span style="color: #0000BB">$data</span><span style="color: #007700">): </span><span style="color: #0000BB">void<br />    </span><span style="color: #007700">{<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">data </span><span style="color: #007700">= </span><span style="color: #0000BB">$data</span><span style="color: #007700">;<br />    }<br /><br />    public function </span><span style="color: #0000BB">bsonSerialize</span><span style="color: #007700">(): array<br />    {<br />        return [</span><span style="color: #DD0000">'foo' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">foo</span><span style="color: #007700">, </span><span style="color: #DD0000">'prot' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prot</span><span style="color: #007700">];<br />    }<br />} </span><span style="color: #FF8000">// =&gt; {"foo": 42, "prot": "wine", "__pclass": {"$type": "80", "$binary": "VXBwZXJDbGFzcw=="}}<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

    </div>
   </div>
  </div>

  <div class="section" id="mongodb.persistence.deserialization">
   <h2 class="title">Deserialization from BSON</h2>

   
   <div class="warning"><strong class="warning">Увага</strong>
    <p class="simpara">
     Технічно, BSON-документи можуть містити повторювані ключі, оскільки
     зберігаються, як списки пар ключ-значення. Однак програми не повинні
     створювати документи із дублікатами ключів, бо поведінка сервера та
     драйвера може бути невизначеною. Також дані можуть бути пошкоджені під час
     декодування BSON-документа, бо в PHP об&#039;єкти не можуть містити
     повторювані назви властивостей, а масиви — дубльовані ключі.
    </p>
   </div>


   <p class="para">
    The legacy <code class="code">mongo</code> extension deserialized
    both BSON documents and arrays as PHP arrays. While PHP arrays are
    convenient to work with, this behavior was problematic because different
    BSON types could deserialize to the same PHP value (e.g.
    <code class="literal">{&quot;0&quot;: &quot;foo&quot;}</code> and <code class="literal">[&quot;foo&quot;]</code>) and make it
    impossible to infer the original BSON type. By default, the
    <code class="code">mongodb</code> extension addresses this concern by ensuring that BSON
    arrays and documents are converted to PHP arrays and objects, respectively.
   </p>
   <p class="para">
    For compound types, there are three data types:
   </p>

   <p class="para">
    <dl>
     
      <dt>root</dt>
      <dd>
       <p class="para">
        refers to the top-level BSON document <em>only</em>
       </p>
      </dd>
     
     
      <dt>document</dt>
      <dd>
       <p class="para">
        refers to embedded BSON documents <em>only</em>
       </p>
      </dd>
     
     
      <dt>array</dt>
      <dd>
       <p class="para">
        refers to a BSON array
       </p>
      </dd>
     
    </dl>
   </p>

   <p class="para">
    Besides the three collective types, it is also possible to configure
    specific fields in your document to map to the data types mentioned below.
    As an example, the following type map allows you to
    map each embedded document within an <code class="literal">&quot;addresses&quot;</code> array to
    an <span class="classname"><strong class="classname">Address</strong></span> class <em>and</em> each
    <code class="literal">&quot;city&quot;</code> field within those embedded address documents to
    a <span class="classname"><strong class="classname">City</strong></span> class:

    <div class="example-contents">
<div class="textcode"><pre class="textcode">[
    &#039;fieldPaths&#039; =&gt; [
        &#039;addresses.$&#039; =&gt; &#039;MyProject\Address&#039;,
        &#039;addresses.$.city&#039; =&gt; &#039;MyProject\City&#039;,
    ],
]</pre>
</div>
    </div>

   </p>

   <p class="para">
    Each of those three data types, as well as the field specific mappings,
    can be mapped against different PHP types. The possible mapping values
    are:
   </p>

   <p class="para">
    <dl>
     
      <dt><em>not set</em> or <span class="type"><a href="language.types.null.php" class="type NULL">NULL</a></span> (default)</dt>
      <dd>
       <p class="para">
        <ul class="itemizedlist">
         <li class="listitem">
          <p class="para">
           A BSON array will be deserialized as a PHP <span class="type"><a href="language.types.array.php" class="type array">array</a></span>.
          </p>
         </li>
         <li class="listitem">
          <p class="para">
           A BSON document (root or embedded) without a
           <span class="property">__pclass</span> property
           <a href="#fnidmongodb.pclass" name="fnmongodb.pclass"><sup>[1]</sup></a>
            
           
           becomes a PHP <span class="classname"><a href="class.stdclass.php" class="classname">stdClass</a></span> object, with each
           BSON document key set as a public <span class="classname"><a href="class.stdclass.php" class="classname">stdClass</a></span>
           property.
          </p>
         </li>
         <li class="listitem">
          <p class="para">
           A BSON document (root or embedded) with a
           <span class="property">__pclass</span> property <a href="#fnidmongodb.pclass"><sup>[1]</sup></a> becomes a PHP object of
           the class name as defined by the <span class="property">__pclass</span>
           property.
          </p>
          <p class="para">
           If the named class implements the
           <span class="interfacename"><a href="class.mongodb-bson-persistable.php" class="interfacename">MongoDB\BSON\Persistable</a></span> interface,
           then the properties of the BSON document, including the
           <span class="property">__pclass</span> property, are sent as an associative
           array to the
           <span class="methodname"><a href="mongodb-bson-unserializable.bsonunserialize.php" class="methodname">MongoDB\BSON\Unserializable::bsonUnserialize()</a></span>
           function to initialise the object&#039;s properties.
          </p>
          <p class="para">
           If the named class does not exist or does not implement the
           <span class="interfacename"><a href="class.mongodb-bson-persistable.php" class="interfacename">MongoDB\BSON\Persistable</a></span> interface,
           <span class="classname"><a href="class.stdclass.php" class="classname">stdClass</a></span> will be used and each BSON document
           key (including <span class="property">__pclass</span>) will be set as a
           public <span class="classname"><a href="class.stdclass.php" class="classname">stdClass</a></span> property.
          </p>
          <p class="para">
           The <span class="property">__pclass</span> functionality relies on the
           property being part of a retrieved MongoDB document. If you use a
           <a href="mongodb-driver-query.construct.php#mongodb-driver-query.construct-queryOptions" class="link">projection</a>
           when querying for documents, you need to include the
           <span class="property">__pclass</span> field in the projection for this
           functionality to work.
          </p>
         </li>
        </ul>
       </p>
      </dd>
     

     
      <dt><code class="literal">&quot;array&quot;</code></dt>
      <dd>
       <p class="para">
        Turns a BSON array or BSON document into a PHP array. There will be no
        special treatment of a <span class="property">__pclass</span> property <a href="#fnidmongodb.pclass"><sup>[1]</sup></a>,
        but it may be set as an element in the returned array if it was
        present in the BSON document.
       </p>
      </dd>
     

     
      <dt><code class="literal">&quot;object&quot;</code> or <code class="literal">&quot;stdClass&quot;</code></dt>
      <dd>
       <p class="para">
        Turns a BSON array or BSON document into a
        <span class="classname"><a href="class.stdclass.php" class="classname">stdClass</a></span> object. There will be no special
        treatment of a <span class="property">__pclass</span> property <a href="#fnidmongodb.pclass"><sup>[1]</sup></a>, but it may
        be set as a public property in the returned object if it was present
        in the BSON document.
       </p>
      </dd>
     

     
      <dt><code class="literal">&quot;bson&quot;</code></dt>
      <dd>
       <p class="para">
        Turns a BSON array into a <span class="classname"><a href="class.mongodb-bson-packedarray.php" class="classname">MongoDB\BSON\PackedArray</a></span>
        and a BSON document into a <span class="classname"><a href="class.mongodb-bson-document.php" class="classname">MongoDB\BSON\Document</a></span>,
        regardless of whether the BSON document has a <span class="property">__pclass</span>
        property <a href="#fnidmongodb.pclass"><sup>[1]</sup></a>.
       </p>
       <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
        <span class="simpara">
         The <code class="literal">bson</code> value is only available for the three root
         types, not in the field specific mappings.
        </span>
       </p></blockquote>
      </dd>
     

     
      <dt>any other string</dt>
      <dd>
       <p class="para">
        Defines the class name that the BSON array or BSON object should be
        deserialized as. For BSON objects that include
        <span class="property">__pclass</span> properties, that class will take
        priority.
       </p>

       <p class="para">
        If the named class does not exist, is not concrete (i.e. it is
        abstract or an interface), or does not implement
        <span class="interfacename"><a href="class.mongodb-bson-unserializable.php" class="interfacename">MongoDB\BSON\Unserializable</a></span> then an
        <span class="classname"><a href="class.mongodb-driver-exception-invalidargumentexception.php" class="classname">MongoDB\Driver\Exception\InvalidArgumentException</a></span>
        exception is thrown.
       </p>

       <p class="para">
        If the BSON object has a <span class="property">__pclass</span> property and
        that class exists and implements
        <span class="interfacename"><a href="class.mongodb-bson-persistable.php" class="interfacename">MongoDB\BSON\Persistable</a></span> it will
        supersede the class provided in the type map.
       </p>

       <p class="para">
        The properties of the BSON document, <em>including</em>
        the <span class="property">__pclass</span> property if it exists, will be sent
        as an associative array to the
        <span class="methodname"><a href="mongodb-bson-unserializable.bsonunserialize.php" class="methodname">MongoDB\BSON\Unserializable::bsonUnserialize()</a></span>
        function to initialise the object&#039;s properties.
       </p>
      </dd>
     
    </dl>
   </p>

   <div class="section" id="mongodb.persistence.typemaps">
    <h2 class="title">TypeMaps</h2>

     <p class="para">
      TypeMaps can be set through the
      <span class="methodname"><a href="mongodb-driver-cursor.settypemap.php" class="methodname">MongoDB\Driver\Cursor::setTypeMap()</a></span> method on a
      <span class="classname"><a href="class.mongodb-driver-cursor.php" class="classname">MongoDB\Driver\Cursor</a></span> object, or the
      <code class="literal">$typeMap</code> argument of
      <span class="function"><a href="function.mongodb.bson-tophp.php" class="function">MongoDB\BSON\toPHP()</a></span>,
      <span class="methodname"><a href="mongodb-bson-document.tophp.php" class="methodname">MongoDB\BSON\Document::toPHP()</a></span>, and
      <span class="methodname"><a href="mongodb-bson-packedarray.tophp.php" class="methodname">MongoDB\BSON\PackedArray::toPHP()</a></span>. Each of the three
      classes (<em>root</em>, <em>document</em>, and
      <em>array</em>) can be individually set, in addition to the
      field specific types.
     </p>

     <p class="para">
      If the value in the map is <span class="type"><a href="language.types.null.php" class="type NULL">NULL</a></span>, it means the same as the
      <em>default</em> value for that item.
     </p>
    </div>

    <div class="section">
     <h2 class="title">Examples</h2>

     <p class="para">
      These examples use the following classes:
     </p>

     <p class="para">
      <dl>
       
        <dt>MyClass</dt>
        <dd>
         <p class="para">
          which does <em>not</em> implement any interface
         </p>
        </dd>
       
       
        <dt>YourClass</dt>
        <dd>
         <p class="para">
          which implements
          <span class="interfacename"><a href="class.mongodb-bson-unserializable.php" class="interfacename">MongoDB\BSON\Unserializable</a></span>
         </p>
        </dd>
       
       
        <dt>OurClass</dt>
        <dd>
         <p class="para">
          which implements
          <span class="interfacename"><a href="class.mongodb-bson-persistable.php" class="interfacename">MongoDB\BSON\Persistable</a></span>
         </p>
        </dd>
       
       
        <dt>TheirClass</dt>
        <dd>
         <p class="para">
          which extends OurClass
         </p>
        </dd>
       
      </dl>
     </p>

     <p class="para">
      The <span class="methodname"><a href="mongodb-bson-unserializable.bsonunserialize.php" class="methodname">MongoDB\BSON\Unserializable::bsonUnserialize()</a></span>
      method of YourClass, OurClass, TheirClass iterate over the array and set
      the properties without modifications. It <em>also</em> sets
      the <code class="literal">$unserialized</code> property to <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>:

      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">function </span><span style="color: #0000BB">bsonUnserialize</span><span style="color: #007700">( array </span><span style="color: #0000BB">$map </span><span style="color: #007700">)<br />{<br />    foreach ( </span><span style="color: #0000BB">$map </span><span style="color: #007700">as </span><span style="color: #0000BB">$k </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$value </span><span style="color: #007700">)<br />    {<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">$k </span><span style="color: #007700">= </span><span style="color: #0000BB">$value</span><span style="color: #007700">;<br />    }<br />    </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">unserialized </span><span style="color: #007700">= </span><span style="color: #0000BB">true</span><span style="color: #007700">;<br />}</span></span></code></div>
      </div>

     </p>

     <p class="para">
      <div class="example-contents">
<div class="textcode"><pre class="textcode">/* typemap: [] (all defaults) */
{ &quot;foo&quot;: &quot;yes&quot;, &quot;bar&quot; : false }
  -&gt; stdClass { $foo =&gt; &#039;yes&#039;, $bar =&gt; false }

{ &quot;foo&quot;: &quot;no&quot;, &quot;array&quot; : [ 5, 6 ] }
  -&gt; stdClass { $foo =&gt; &#039;no&#039;, $array =&gt; [ 5, 6 ] }

{ &quot;foo&quot;: &quot;no&quot;, &quot;obj&quot; : { &quot;embedded&quot; : 3.14 } }
  -&gt; stdClass { $foo =&gt; &#039;no&#039;, $obj =&gt; stdClass { $embedded =&gt; 3.14 } }

{ &quot;foo&quot;: &quot;yes&quot;, &quot;__pclass&quot;: &quot;MyClass&quot; }
  -&gt; stdClass { $foo =&gt; &#039;yes&#039;, $__pclass =&gt; &#039;MyClass&#039; }

{ &quot;foo&quot;: &quot;yes&quot;, &quot;__pclass&quot;: { &quot;$type&quot; : &quot;80&quot;, &quot;$binary&quot; : &quot;MyClass&quot; } }
  -&gt; stdClass { $foo =&gt; &#039;yes&#039;, $__pclass =&gt; Binary(0x80, &#039;MyClass&#039;) }

{ &quot;foo&quot;: &quot;yes&quot;, &quot;__pclass&quot;: { &quot;$type&quot; : &quot;80&quot;, &quot;$binary&quot; : &quot;YourClass&quot;) }
  -&gt; stdClass { $foo =&gt; &#039;yes&#039;, $__pclass =&gt; Binary(0x80, &#039;YourClass&#039;) }

{ &quot;foo&quot;: &quot;yes&quot;, &quot;__pclass&quot;: { &quot;$type&quot; : &quot;80&quot;, &quot;$binary&quot; : &quot;OurClass&quot;) }
  -&gt; OurClass { $foo =&gt; &#039;yes&#039;, $__pclass =&gt; Binary(0x80, &#039;OurClass&#039;), $unserialized =&gt; true }

{ &quot;foo&quot;: &quot;yes&quot;, &quot;__pclass&quot;: { &quot;$type&quot; : &quot;44&quot;, &quot;$binary&quot; : &quot;YourClass&quot;) }
  -&gt; stdClass { $foo =&gt; &#039;yes&#039;, $__pclass =&gt; Binary(0x44, &#039;YourClass&#039;) }</pre>
</div>
      </div>

     </p>

     <p class="para">
      <div class="example-contents">
<div class="textcode"><pre class="textcode">/* typemap: [ &quot;root&quot; =&gt; &quot;MissingClass&quot; ] */
{ &quot;foo&quot;: &quot;yes&quot; }
  -&gt; MongoDB\Driver\Exception\InvalidArgumentException(&quot;MissingClass does not exist&quot;)

/* typemap: [ &quot;root&quot; =&gt; &quot;MyClass&quot; ] */
{ &quot;foo&quot;: &quot;yes&quot;, &quot;__pclass&quot; : { &quot;$type&quot;: &quot;80&quot;, &quot;$binary&quot;: &quot;MyClass&quot; } }
  -&gt; MongoDB\Driver\Exception\InvalidArgumentException(&quot;MyClass does not implement Unserializable interface&quot;)

/* typemap: [ &quot;root&quot; =&gt; &quot;MongoDB\BSON\Unserializable&quot; ] */
{ &quot;foo&quot;: &quot;yes&quot; }
  -&gt; MongoDB\Driver\Exception\InvalidArgumentException(&quot;Unserializable is not a concrete class&quot;)

/* typemap: [ &quot;root&quot; =&gt; &quot;YourClass&quot; ] */
{ &quot;foo&quot;: &quot;yes&quot;, &quot;__pclass&quot; : { &quot;$type&quot;: &quot;80&quot;, &quot;$binary&quot;: &quot;MongoDB\BSON\Unserializable&quot; } }
  -&gt; YourClass { $foo =&gt; &quot;yes&quot;, $__pclass =&gt; Binary(0x80, &quot;MongoDB\BSON\Unserializable&quot;), $unserialized =&gt; true }

/* typemap: [ &quot;root&quot; =&gt; &quot;YourClass&quot; ] */
{ &quot;foo&quot;: &quot;yes&quot;, &quot;__pclass&quot; : { &quot;$type&quot;: &quot;80&quot;, &quot;$binary&quot;: &quot;MyClass&quot; } }
  -&gt; YourClass { $foo =&gt; &quot;yes&quot;, $__pclass =&gt; Binary(0x80, &quot;MyClass&quot;), $unserialized =&gt; true }

/* typemap: [ &quot;root&quot; =&gt; &quot;YourClass&quot; ] */
{ &quot;foo&quot;: &quot;yes&quot;, &quot;__pclass&quot; : { &quot;$type&quot;: &quot;80&quot;, &quot;$binary&quot;: &quot;OurClass&quot; } }
  -&gt; OurClass { $foo =&gt; &quot;yes&quot;, $__pclass =&gt; Binary(0x80, &quot;OurClass&quot;), $unserialized =&gt; true }

/* typemap: [ &quot;root&quot; =&gt; &quot;YourClass&quot; ] */
{ &quot;foo&quot;: &quot;yes&quot;, &quot;__pclass&quot; : { &quot;$type&quot;: &quot;80&quot;, &quot;$binary&quot;: &quot;TheirClass&quot; } }
  -&gt; TheirClass { $foo =&gt; &quot;yes&quot;, $__pclass =&gt; Binary(0x80, &quot;TheirClass&quot;), $unserialized =&gt; true }

/* typemap: [ &quot;root&quot; =&gt; &quot;OurClass&quot; ] */
{ foo: &quot;yes&quot;, &quot;__pclass&quot; : { &quot;$type&quot;: &quot;80&quot;, &quot;$binary&quot;: &quot;TheirClass&quot; } }
  -&gt; TheirClass { $foo =&gt; &quot;yes&quot;, $__pclass =&gt; Binary(0x80, &quot;TheirClass&quot;), $unserialized =&gt; true }</pre>
</div>
      </div>

     </p>

     <p class="para">
      <div class="example-contents">
<div class="textcode"><pre class="textcode">/* typemap: [ &#039;root&#039; =&gt; &#039;YourClass&#039; ] */
{ foo: &quot;yes&quot;, &quot;__pclass&quot; : { &quot;$type&quot;: &quot;80&quot;, &quot;$binary&quot;: &quot;YourClass&quot; } }
  -&gt; YourClass { $foo =&gt; &#039;yes&#039;, $__pclass =&gt; Binary(0x80, &#039;YourClass&#039;), $unserialized =&gt; true }</pre>
</div>
      </div>

     </p>

     <p class="para">
      <div class="example-contents">
<div class="textcode"><pre class="textcode">/* typemap: [ &#039;root&#039; =&gt; &#039;array&#039;, &#039;document&#039; =&gt; &#039;array&#039; ] */
{ &quot;foo&quot;: &quot;yes&quot;, &quot;bar&quot; : false }
  -&gt; [ &quot;foo&quot; =&gt; &quot;yes&quot;, &quot;bar&quot; =&gt; false ]

{ &quot;foo&quot;: &quot;no&quot;, &quot;array&quot; : [ 5, 6 ] }
  -&gt; [ &quot;foo&quot; =&gt; &quot;no&quot;, &quot;array&quot; =&gt; [ 5, 6 ] ]

{ &quot;foo&quot;: &quot;no&quot;, &quot;obj&quot; : { &quot;embedded&quot; : 3.14 } }
  -&gt; [ &quot;foo&quot; =&gt; &quot;no&quot;, &quot;obj&quot; =&gt; [ &quot;embedded =&gt; 3.14 ] ]

{ &quot;foo&quot;: &quot;yes&quot;, &quot;__pclass&quot;: &quot;MyClass&quot; }
  -&gt; [ &quot;foo&quot; =&gt; &quot;yes&quot;, &quot;__pclass&quot; =&gt; &quot;MyClass&quot; ]

{ &quot;foo&quot;: &quot;yes&quot;, &quot;__pclass&quot; : { &quot;$type&quot;: &quot;80&quot;, &quot;$binary&quot;: &quot;MyClass&quot; } }
  -&gt; [ &quot;foo&quot; =&gt; &quot;yes&quot;, &quot;__pclass&quot; =&gt; Binary(0x80, &quot;MyClass&quot;) ]

{ &quot;foo&quot;: &quot;yes&quot;, &quot;__pclass&quot; : { &quot;$type&quot;: &quot;80&quot;, &quot;$binary&quot;: &quot;OurClass&quot; } }
  -&gt; [ &quot;foo&quot; =&gt; &quot;yes&quot;, &quot;__pclass&quot; =&gt; Binary(0x80, &quot;OurClass&quot;) ]</pre>
</div>
      </div>

     </p>

     <p class="para">
      <div class="example-contents">
<div class="textcode"><pre class="textcode">/* typemap: [ &#039;root&#039; =&gt; &#039;object&#039;, &#039;document&#039; =&gt; &#039;object&#039; ] */
{ &quot;foo&quot;: &quot;yes&quot;, &quot;__pclass&quot;: { &quot;$type&quot;: &quot;80&quot;, &quot;$binary&quot;: &quot;MyClass&quot; } }
  -&gt; stdClass { $foo =&gt; &quot;yes&quot;, &quot;__pclass&quot; =&gt; Binary(0x80, &quot;MyClass&quot;) }</pre>
</div>
      </div>

     </p>

   </div>
  </div>

 </div><?php manual_footer($setup); ?>