Get value of dynamically chosen class constant in PHP
Use the constant() function: $id = constant(“ThingIDs::$thing”);
Use the constant() function: $id = constant(“ThingIDs::$thing”);
Constant pool is a part of .class file (and its in-memory representation) that contains constants needed to run the code of that class. These constants include literals specified by the programmer and symbolic references generated by compiler. Symbolic references are basically names of classes, methods and fields referenced from the code. These references are used … Read more
This is advanced behavior which will not be needed in 90+% of the enumerations created. According to the docs: The rules for what is allowed are as follows: _sunder_ names (starting and ending with a single underscore) are reserved by enum and cannot be used; all other attributes defined within an enumeration will become members … Read more
You can use Reflection for this. Note that if you are doing this a lot you may want to looking at caching the result. <?php class Profile { const LABEL_FIRST_NAME = “First Name”; const LABEL_LAST_NAME = “Last Name”; const LABEL_COMPANY_NAME = “Company”; } $refl = new ReflectionClass(‘Profile’); print_r($refl->getConstants()); Output: Array ( ‘LABEL_FIRST_NAME’ => ‘First Name’, … Read more