<?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 => 'fr',
  ),
  'this' => 
  array (
    0 => 'language.enumerations.expressions.php',
    1 => 'Valeurs d\'&eacute;num&eacute;ration dans les expressions constantes',
    2 => 'Valeurs d\'&eacute;num&eacute;ration dans les expressions constantes',
  ),
  'up' => 
  array (
    0 => 'language.enumerations.php',
    1 => 'Les &eacute;num&eacute;rations',
  ),
  'prev' => 
  array (
    0 => 'language.enumerations.traits.php',
    1 => 'Traits',
  ),
  'next' => 
  array (
    0 => 'language.enumerations.object-differences.php',
    1 => 'Diff&eacute;rences avec les objets',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    '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">Valeurs d&#039;énumération dans les expressions constantes</h2>

  <p class="para">
   Les cas étant représentés comme des constantes dans l&#039;énumération elle-même, ils peuvent être utilisés comme des valeurs statiques
   dans la plupart des expressions constantes : valeurs par défaut des propriétés, valeurs par défaut des variables statiques,
   valeurs par défaut des paramètres, valeurs constantes globales et de classe. Elles ne peuvent pas être utilisées dans d&#039;autres valeurs de cas de l&#039;enum, mais les
   constantes normales peuvent faire référence à un cas d&#039;énumération.
  </p>

  <p class="para">
   Cependant, les appels implicites à des méthodes magiques telles que <span class="classname"><a href="class.arrayaccess.php" class="classname">ArrayAccess</a></span> sur les enums ne sont pas autorisés dans les définitions statiques 
   ou constantes, car nous ne pouvons pas garantir de manière absolue que la valeur résultante est déterministe
   ou que l&#039;invocation de la méthode est exempte d&#039;effets secondaires. Les appels de fonction, les appels de méthode et
   l&#039;accès aux propriétés continuent d&#039;être des opérations non valides dans les expressions constantes.
  </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">// Ceci est une définition d'Enum tout à fait valide.<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">// Ceci est autorisé.<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">// Ceci n'est pas autorisé, car le résultat peut ne pas être déterministe.<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">// Erreur fatale : Cannot use [] on enums in constant expression<br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #FF8000">// Ceci est tout à fait légal, car ce n'est pas une expression constante.<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); ?>