Php get all constants from class

Get the latest constants declared.

abstract class AbstractEnum
{
    /**
     * Возвращает все константы класса || Return all constants
     *
     * @return array
     */
    static function getConstants()
    {
        $rc = new \ReflectionClass(get_called_class());

        return $rc->getConstants();
    }

    /**
     * Возвращает массив констант определенные в вызываемом классе  || Return last constants
     *
     * @return array
     */
    static function lastConstants()
    {
        $parentConstants = static::getParentConstants();

        $allConstants = static::getConstants();

        return array_diff($allConstants, $parentConstants);
    }

    /**
     * Возвращает все константы родительских классов || Return parent constants
     *
     * @return array
     */
    static function getParentConstants()
    {
        $rc = new \ReflectionClass(get_parent_class(static::class));
        $consts = $rc->getConstants();

        return $consts;
    }
}

======
class Roles extends AbstractEnum
{
    const ROOT = 'root';
    const ADMIN = 'admin';
    const USER = 'user';
}

// Output:
All: root, admin, user
Last: root, admin, user

class NewRoles extends Roles
{
    const CLIENT = 'client';
    const MODERATOR = 'moderator';
    const SUPERMODERATOR = 'super'.self::USER;
}

// Output:
All: client, moderator, superuser, root, admin, user
Last: client, moderator, superuser

class AdditionalRoles extends Roles
{
    const VIEWER = 'viewer';
    const CHECKER = 'checker';
    const ROOT = 'rooter';
}

All: viewer, checker, rooter, client, moderator, superuser, admin, user
Last: viewer, checker, rooter

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')

Home / Get a list of all available constants with PHP

PHP has the function get_defined_constants() which allows you to get a list of all the available constants in an array. This post looks at how to use the get_defined_constants() function and example output from it.

The get_defined_constants() function takes one optional parameter, which specifies whether or not to categorise the constants. The default is false, which means an associative array is returned containing the constant name as the key and the constant value as the value. If the categorise parameter is set to true, a multi-dimensional array is returned containing the section names as the index and then a sub array of name-value pairs in the second dimension.

For example:

print_r(get_defined_constants());

will output something like this:

Array
(
    [E_ERROR] => 1
    [E_WARNING] => 2
    [E_PARSE] => 4
    [E_NOTICE] => 8
    [E_STRICT] => 2048
    [E_CORE_ERROR] => 16
    [E_CORE_WARNING] => 32
    [E_COMPILE_ERROR] => 64
    [E_COMPILE_WARNING] => 128
    [E_USER_ERROR] => 256
    [E_USER_WARNING] => 512
    [E_USER_NOTICE] => 1024
    [E_ALL] => 2047
    [TRUE] => 1
    [FALSE] => 
    [NULL] => 
    [ZEND_THREAD_SAFE] => 
    [PHP_VERSION] => 5.1.6
    [PHP_OS] => Linux
    [PHP_SAPI] => apache2handler
    [DEFAULT_INCLUDE_PATH] => .:/usr/share/pear
...

and

print_r(get_defined_constants(true));

will output something like this:

Array
(
    [internal] => Array
        (
            [E_ERROR] => 1
            [E_WARNING] => 2
            ...
        )
    [libxml] => Array
        (
            [LIBXML_VERSION] => 20626
            [LIBXML_DOTTED_VERSION] => 2.6.26
            ...
        )
    [xml] => Array
        (
            [XML_ERROR_NONE] => 0
            [XML_ERROR_NO_MEMORY] => 1
            ...
         )
    ...
)

If you have any user defined constants they will be listed at the end when not categorised, and in a [user] section when categorised.