How to Retrieve Defined CONSTs on a PHP Class
Question:
How can one obtain a list of CONSTs defined within a PHP class? Using the get_defined_constants() function proves insufficient.
Answer:
Leveraging the ReflectionClass interface provides a solution to this query. Repeated executions of this process may benefit from caching the resulting data.
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' )
The above is the detailed content of How to Get a List of Defined Constants in a PHP Class?. For more information, please follow other related articles on the PHP Chinese website!