Can I get CONST's defined on a PHP class?
class-constants, constants, php
Solution
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',
'LABEL_LAST_NAME' => 'Last Name',
'LABEL_COMPANY_NAME' => 'Company'
)
Problem
I have several CONST's defined on some classes, and want to get a list of them. For example: ``` class Profile { const LABEL_FIRST_NAME = "First Name"; const LABEL_LAST_NAME = "Last Name"; const LABEL_COMPANY_NAME = "Company"; } ``` Is there any way to get a list of the CONST's defined on the `Profile` class? As far as I can tell, the closest option(`get_defined_constants()`) won't do the trick. What I actually need is a list of the constant names - something like this: ``` array('LABEL_FIRST_NAME', 'LABEL_LAST_NAME', 'LABEL_COMPANY_NAME') ``` Or: ``` array('Profile::LABEL_FIRST_NAME', 'Profile::LABEL_LAST_NAME', 'Profile::LABEL_COMPANY_NAME') ``` Or even: ``` array('Profile::LABEL_FIRST_NAME'=>'First Name', 'Profile::LABEL_LAST_NAME'=>'Last Name', 'Profile::LABEL_COMPANY_NAME'=>'Company') ```