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

  <p class="para">
   Although Enums are built on classes and objects, they do not support all object-related functionality.
   In particular, Enum cases are forbidden from having state.
  </p>

  <ul class="simplelist">
   <li>Constructors and Destructors are forbidden.</li>
   <li>Inheritance is not supported. Enums may not extend or be extended.</li>
   <li>Static or object properties are not allowed.</li>
   <li>Cloning an Enum case is not supported, as cases must be singleton instances.</li>
   <li><a href="language.oop5.magic.php" class="link">Magic methods</a>, except for those listed below, are disallowed.</li>
   <li>Enums must always be declared before they are used.</li>
  </ul>

  <p class="para">The following object functionality is available, and behaves just as it does on any other object:</p>

  <ul class="simplelist">
   <li>Public, private, and protected methods.</li>
   <li>Public, private, and protected static methods.</li>
   <li>Public, private, and protected constants.</li>
   <li>Enums may implement any number of interfaces.</li>
   <li>
    Enums and cases may have <a href="language.attributes.php" class="link">attributes</a> attached
    to them. The <strong><code>TARGET_CLASS</code></strong> target
    filter includes Enums themselves. The <strong><code>TARGET_CLASS_CONST</code></strong> target filter
    includes Enum Cases.
   </li>
   <li>
    <a href="language.oop5.overloading.php#object.call" class="link">__call</a>, <a href="language.oop5.overloading.php#object.callstatic" class="link">__callStatic</a>,
    and <a href="language.oop5.magic.php#object.invoke" class="link">__invoke</a> magic methods
   </li>
   <li><strong><code><a href="language.constants.magic.php#constant.class">__CLASS__</a></code></strong> and <strong><code><a href="language.constants.magic.php#constant.function">__FUNCTION__</a></code></strong> constants behave as normal</li>
  </ul>

  <p class="para">
   The <code class="literal">::class</code> magic constant on an Enum type evaluates to the type
   name including any namespace, exactly the same as an object. The <code class="literal">::class</code>
   magic constant on a Case instance also evaluates to the Enum type, as it is an
   instance of that type.
  </p>

  <p class="para">
   Additionally, enum cases may not be instantiated directly with <code class="literal">new</code>, nor with
   <span class="methodname"><a href="reflectionclass.newinstancewithoutconstructor.php" class="methodname">ReflectionClass::newInstanceWithoutConstructor()</a></span> in reflection. Both will result in an error.
  </p>

  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$clovers </span><span style="color: #007700">= new </span><span style="color: #0000BB">Suit</span><span style="color: #007700">();<br /></span><span style="color: #FF8000">// Error: Cannot instantiate enum Suit<br /><br /></span><span style="color: #0000BB">$horseshoes </span><span style="color: #007700">= (new </span><span style="color: #0000BB">ReflectionClass</span><span style="color: #007700">(</span><span style="color: #0000BB">Suit</span><span style="color: #007700">::class))-&gt;</span><span style="color: #0000BB">newInstanceWithoutConstructor</span><span style="color: #007700">()<br /></span><span style="color: #FF8000">// Error: Cannot instantiate enum Suit<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
  </div>

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