<?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 => 'uk',
  ),
  'this' => 
  array (
    0 => 'language.enumerations.expressions.php',
    1 => 'Enum values in constant expressions',
    2 => 'Enum values in constant expressions',
  ),
  'up' => 
  array (
    0 => 'language.enumerations.php',
    1 => 'Enumerations',
  ),
  'prev' => 
  array (
    0 => 'language.enumerations.traits.php',
    1 => 'Traits',
  ),
  'next' => 
  array (
    0 => 'language.enumerations.object-differences.php',
    1 => 'Differences from objects',
  ),
  '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.expressions" class="sect1">
  <h2 class="title">Enum values in constant expressions</h2>

  <p class="para">
   Because cases are represented as constants on the enum itself, they may be used as static
   values in most constant expressions: property defaults, static variable defaults, parameter
   defaults, global and class constant values. They may not be used in other enum case values, but
   normal constants may refer to an enum case.
  </p>

  <p class="para">
   However, implicit magic method calls such as <span class="classname"><a href="class.arrayaccess.php" class="classname">ArrayAccess</a></span> on enums are not allowed in static
   or constant definitions as we cannot absolutely guarantee that the resulting value is deterministic
   or that the method invocation is free of side effects. Function calls, method calls, and
   property access continue to be invalid operations in constant expressions.
  </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: #FF8000">// This is an entirely legal Enum definition.<br /></span><span style="color: #007700">enum </span><span style="color: #0000BB">Direction </span><span style="color: #007700">implements </span><span style="color: #0000BB">ArrayAccess<br /></span><span style="color: #007700">{<br />    case </span><span style="color: #0000BB">Up</span><span style="color: #007700">;<br />    case </span><span style="color: #0000BB">Down</span><span style="color: #007700">;<br /><br />    public function </span><span style="color: #0000BB">offsetExists</span><span style="color: #007700">(</span><span style="color: #0000BB">$offset</span><span style="color: #007700">): </span><span style="color: #0000BB">bool<br />    </span><span style="color: #007700">{<br />        return </span><span style="color: #0000BB">false</span><span style="color: #007700">;<br />    }<br /><br />    public function </span><span style="color: #0000BB">offsetGet</span><span style="color: #007700">(</span><span style="color: #0000BB">$offset</span><span style="color: #007700">): </span><span style="color: #0000BB">mixed<br />    </span><span style="color: #007700">{<br />        return </span><span style="color: #0000BB">null</span><span style="color: #007700">;<br />    }<br /><br />    public function </span><span style="color: #0000BB">offsetSet</span><span style="color: #007700">(</span><span style="color: #0000BB">$offset</span><span style="color: #007700">, </span><span style="color: #0000BB">$value</span><span style="color: #007700">): </span><span style="color: #0000BB">void<br />    </span><span style="color: #007700">{<br />        throw new </span><span style="color: #0000BB">Exception</span><span style="color: #007700">();<br />    }<br /><br />    public function </span><span style="color: #0000BB">offsetUnset</span><span style="color: #007700">(</span><span style="color: #0000BB">$offset</span><span style="color: #007700">): </span><span style="color: #0000BB">void<br />    </span><span style="color: #007700">{<br />        throw new </span><span style="color: #0000BB">Exception</span><span style="color: #007700">();<br />    }<br />}<br /><br />class </span><span style="color: #0000BB">Foo<br /></span><span style="color: #007700">{<br />    </span><span style="color: #FF8000">// This is allowed.<br />    </span><span style="color: #007700">const </span><span style="color: #0000BB">DOWN </span><span style="color: #007700">= </span><span style="color: #0000BB">Direction</span><span style="color: #007700">::</span><span style="color: #0000BB">Down</span><span style="color: #007700">;<br /><br />    </span><span style="color: #FF8000">// This is disallowed, as it may not be deterministic.<br />    </span><span style="color: #007700">const </span><span style="color: #0000BB">UP </span><span style="color: #007700">= </span><span style="color: #0000BB">Direction</span><span style="color: #007700">::</span><span style="color: #0000BB">Up</span><span style="color: #007700">[</span><span style="color: #DD0000">'short'</span><span style="color: #007700">];<br />    </span><span style="color: #FF8000">// Fatal error: Cannot use [] on enums in constant expression<br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #FF8000">// This is entirely legal, because it's not a constant expression.<br /></span><span style="color: #0000BB">$x </span><span style="color: #007700">= </span><span style="color: #0000BB">Direction</span><span style="color: #007700">::</span><span style="color: #0000BB">Up</span><span style="color: #007700">[</span><span style="color: #DD0000">'short'</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #DD0000">"\$x is " </span><span style="color: #007700">. </span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">$x</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= new </span><span style="color: #0000BB">Foo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
  </div>

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