<?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 => 'de',
  ),
  'this' => 
  array (
    0 => 'language.enumerations.expressions.php',
    1 => 'Enum-Werte in konstanten Ausdr&uuml;cken',
    2 => 'Enum-Werte in konstanten Ausdr&uuml;cken',
  ),
  'up' => 
  array (
    0 => 'language.enumerations.php',
    1 => 'Aufz&auml;hlungen (Enum)',
  ),
  'prev' => 
  array (
    0 => 'language.enumerations.traits.php',
    1 => 'Traits',
  ),
  'next' => 
  array (
    0 => 'language.enumerations.object-differences.php',
    1 => 'Unterschiede zu Objekten',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'de',
    '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-Werte in konstanten Ausdrücken</h2>

  <p class="para">
   Da die Fälle in der Enum selbst als Konstanten dargestellt werden, können
   sie in den meisten konstanten Ausdrücken als statische Werte verwendet
   werden: als Standardwerte für Eigenschaften, statische Variablen und
   Parameter und als globale Werte und Klassenkonstanten. Sie dürfen nicht in
   den Werten anderer Enum-Fälle verwendet werden, aber normale Konstanten
   können sich auf einen Enum-Fall beziehen.
  </p>

  <p class="para">
   Dagegen sind implizite Aufrufe magischer Methoden wie
   <span class="classname"><a href="class.arrayaccess.php" class="classname">ArrayAccess</a></span> in Enums in statischen oder konstanten
   Definitionen nicht erlaubt, da nicht absolut garantiert werden kann, dass
   der resultierende Wert deterministisch ist oder dass der Methodenaufruf
   frei von Seiteneffekten ist. Funktionsaufrufe, Methodenaufrufe und der
   Zugriff auf Eigenschaften sind in konstanten Ausdrücken weiterhin nicht
   erlaubt.
  </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">// Dies ist eine völlig zulässige Definition einer Enum.<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">// Dies ist erlaubt.<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">// Dies ist unzulässig, da es nicht deterministisch sein kann.<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">// Dies ist völlig zulässig, da es kein konstanter Ausdruck ist.<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); ?>