<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.enumerations.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'language.enumerations.serialization.php',
    1 => 'Serialization',
    2 => 'Serialization',
  ),
  'up' => 
  array (
    0 => 'language.enumerations.php',
    1 => 'Enumerations',
  ),
  'prev' => 
  array (
    0 => 'language.enumerations.listing.php',
    1 => 'Value listing',
  ),
  'next' => 
  array (
    0 => 'language.enumerations.object-differences.inheritance.php',
    1 => 'Why enums aren\'t extendable',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/enumerations.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="language.enumerations.serialization" class="sect1">
  <h2 class="title">Serialization</h2>

  <p class="para">
   Enumerations are serialized differently from objects. Specifically, they have a new serialization code,
   <code class="literal">&quot;E&quot;</code>, that specifies the name of the enum case. The deserialization routine is then
   able to use that to set a variable to the existing singleton value. That ensures that:
  </p>

  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />Suit</span><span style="color: #007700">::</span><span style="color: #0000BB">Hearts </span><span style="color: #007700">=== </span><span style="color: #0000BB">unserialize</span><span style="color: #007700">(</span><span style="color: #0000BB">serialize</span><span style="color: #007700">(</span><span style="color: #0000BB">Suit</span><span style="color: #007700">::</span><span style="color: #0000BB">Hearts</span><span style="color: #007700">));<br /><br />print </span><span style="color: #0000BB">serialize</span><span style="color: #007700">(</span><span style="color: #0000BB">Suit</span><span style="color: #007700">::</span><span style="color: #0000BB">Hearts</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// E:11:"Suit:Hearts";<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
  </div>


  <p class="para">
   On deserialization, if an enum and case cannot be found to match a serialized
   value a warning will be issued and <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> returned.</p>

  <p class="para">
   If a Pure Enum is serialized to JSON, an error will be thrown. If a Backed Enum
   is serialized to JSON, it will be represented by its scalar value only, in the
   appropriate type. The behavior of both may be overridden by implementing
   <span class="classname"><a href="class.jsonserializable.php" class="classname">JsonSerializable</a></span>.
  </p>

  <p class="para">For <span class="function"><a href="function.print-r.php" class="function">print_r()</a></span>, the output of an enum case is slightly different
   from objects to minimize confusion.
  </p>

  <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">enum </span><span style="color: #0000BB">Foo </span><span style="color: #007700">{<br />    case </span><span style="color: #0000BB">Bar</span><span style="color: #007700">;<br />}<br /><br />enum </span><span style="color: #0000BB">Baz</span><span style="color: #007700">: </span><span style="color: #0000BB">int </span><span style="color: #007700">{<br />    case </span><span style="color: #0000BB">Beep </span><span style="color: #007700">= </span><span style="color: #0000BB">5</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">Foo</span><span style="color: #007700">::</span><span style="color: #0000BB">Bar</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">Baz</span><span style="color: #007700">::</span><span style="color: #0000BB">Beep</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* Produces<br /><br />Foo Enum (<br />    [name] =&gt; Bar<br />)<br />Baz Enum:int {<br />    [name] =&gt; Beep<br />    [value] =&gt; 5<br />}<br />*/<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
  </div>

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