Differences from objects
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.
- Constructors and Destructors are forbidden.
- Inheritance is not supported. Enums may not extend or be extended.
- Static or object properties are not allowed.
- Cloning an Enum case is not supported, as cases must be singleton instances.
- Magic methods, except for those listed below, are disallowed.
- Enums must always be declared before they are used.
The following object functionality is available, and behaves just as it does on any other object:
- Public, private, and protected methods.
- Public, private, and protected static methods.
- Public, private, and protected constants.
- Enums may implement any number of interfaces.
-
Enums and cases may have attributes attached
to them. The
TARGET_CLASS
target filter includes Enums themselves. TheTARGET_CLASS_CONST
target filter includes Enum Cases. - __call, __callStatic, and __invoke magic methods
__CLASS__
and__FUNCTION__
constants behave as normal
The ::class
magic constant on an Enum type evaluates to the type
name including any namespace, exactly the same as an object. The ::class
magic constant on a Case instance also evaluates to the Enum type, as it is an
instance of that type.
Additionally, enum cases may not be instantiated directly with new
, nor with
ReflectionClass::newInstanceWithoutConstructor() in reflection. Both will result in an error.
<?php
$clovers = new Suit();
// Error: Cannot instantiate enum Suit
$horseshoes = (new ReflectionClass(Suit::class))->newInstanceWithoutConstructor()
// Error: Cannot instantiate enum Suit
?>
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-language.enumerations.object-differences.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.