<?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.object-differences.inheritance.php',
    1 => 'Porque enumera&ccedil;&otilde;es n&atilde;o s&atilde;o extens&iacute;veis',
    2 => 'Porque enumera&ccedil;&otilde;es n&atilde;o s&atilde;o extens&iacute;veis',
  ),
  'up' => 
  array (
    0 => 'language.enumerations.php',
    1 => 'Enumera&ccedil;&otilde;es',
  ),
  'prev' => 
  array (
    0 => 'language.enumerations.serialization.php',
    1 => 'Serializa&ccedil;&atilde;o',
  ),
  'next' => 
  array (
    0 => 'language.enumerations.examples.php',
    1 => 'Exemplos',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'pt_BR',
    '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.object-differences.inheritance" class="sect1">

  <h2 class="title">Why enums aren&#039;t extendable</h2>

  <p class="simpara">
   Classes have contracts on their methods:
  </p>

  <div class="example-contents">
<div class="annotation-non-interactive phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php

</span><span style="color: #007700">class </span><span style="color: #0000BB">A </span><span style="color: #007700">{}
class </span><span style="color: #0000BB">B </span><span style="color: #007700">extends </span><span style="color: #0000BB">A </span><span style="color: #007700">{}

function </span><span style="color: #0000BB">foo</span><span style="color: #007700">(</span><span style="color: #0000BB">A $a</span><span style="color: #007700">) {}

function </span><span style="color: #0000BB">bar</span><span style="color: #007700">(</span><span style="color: #0000BB">B $b</span><span style="color: #007700">)
{
    </span><span style="color: #0000BB">foo</span><span style="color: #007700">(</span><span style="color: #0000BB">$b</span><span style="color: #007700">);
}</span></code></pre></div>
 </div>


  <p class="simpara">
   This code is type-safe, as B follows the contract of A, and through the magic of
   co/contra-variance, any expectation one may have of the methods will be
   preserved, exceptions excepted.
  </p>

  <p class="simpara">
   Enums have contracts on their cases, not methods:
  </p>

  <div class="example-contents">
<div class="annotation-non-interactive phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php

</span><span style="color: #007700">enum </span><span style="color: #0000BB">ErrorCode
</span><span style="color: #007700">{
    case </span><span style="color: #0000BB">SOMETHING_BROKE</span><span style="color: #007700">;
}

function </span><span style="color: #0000BB">quux</span><span style="color: #007700">(</span><span style="color: #0000BB">ErrorCode $errorCode</span><span style="color: #007700">)
{
    </span><span style="color: #FF8000">// When written, this code appears to cover all cases
    </span><span style="color: #007700">match (</span><span style="color: #0000BB">$errorCode</span><span style="color: #007700">) {
        </span><span style="color: #0000BB">ErrorCode</span><span style="color: #007700">::</span><span style="color: #0000BB">SOMETHING_BROKE </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">true</span><span style="color: #007700">,
    };
}</span></code></pre></div>
  </div>


  <p class="simpara">
   The <a href="control-structures.match.php" class="link">match</a> statement in the function <code class="code">quux</code> can be static analyzed to cover
   all of the cases in ErrorCode.
  </p>

  <p class="simpara">
   But imagine it was allowed to extend enums:
  </p>


  <div class="example-contents">
<div class="annotation-non-interactive phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php

</span><span style="color: #FF8000">// Thought experiment code where enums are not final.
// Note, this won't actually work in PHP.
</span><span style="color: #007700">enum </span><span style="color: #0000BB">MoreErrorCode </span><span style="color: #007700">extends </span><span style="color: #0000BB">ErrorCode
</span><span style="color: #007700">{
    case </span><span style="color: #0000BB">PEBKAC</span><span style="color: #007700">;
}

function </span><span style="color: #0000BB">fot</span><span style="color: #007700">(</span><span style="color: #0000BB">MoreErrorCode $errorCode</span><span style="color: #007700">)
{
    </span><span style="color: #0000BB">quux</span><span style="color: #007700">(</span><span style="color: #0000BB">$errorCode</span><span style="color: #007700">);
}

</span><span style="color: #0000BB">fot</span><span style="color: #007700">(</span><span style="color: #0000BB">MoreErrorCode</span><span style="color: #007700">::</span><span style="color: #0000BB">PEBKAC</span><span style="color: #007700">);</span></code></pre></div>
  </div>


  <p class="simpara">
   Under normal inheritance rules, a class that extends another will pass
   the type check.
  </p>

  <p class="simpara">
   The problem would be that the <a href="control-structures.match.php" class="link">match</a> statement in <code class="code">quux()</code> no longer covers all
   the cases. Because it doesn&#039;t know about <code class="code">MoreErrorCode::PEBKAC</code> the match will throw an exception.
  </p>

  <p class="simpara">
   Because of this enums are final and can&#039;t be extended.
  </p>
 </div><?php manual_footer($setup); ?>