Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get CONST's defined on a PHP class?

People also ask

How can I get constant in PHP?

To access the class constant, you use the name of the class (in this case mathematics), followed by 2 colons (::), followed by the name of the constant (in this case PI). In this code, we echo out the constant, so an echo precedes this code. Usually by convention, constants are put in all uppercase.

Can a class be constant?

It is possible to define constants on a per-class basis remaining the same and unchangeable. The default visibility of class constants is public . Note: Class constants can be redefined by a child class.

How do you declare an array constant in PHP?

Array constants can now be defined using the define() function. In PHP 5.6, they could only be defined using const keyword.

Are PHP constants global?

PHP constants are said to have global scope. This basically means that once you have defined a constant it is accessible from any function or object in your script. In addition, PHP provides a number of built-in constants that are available for use to make life easier for the PHP developer.


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

This

 $reflector = new ReflectionClass('Status');
 var_dump($reflector->getConstants());

Use token_get_all(). Namely:

<?php
header('Content-Type: text/plain');

$file = file_get_contents('Profile.php');
$tokens = token_get_all($file);

$const = false;
$name = '';
$constants = array();
foreach ($tokens as $token) {
    if (is_array($token)) {
        if ($token[0] != T_WHITESPACE) {
            if ($token[0] == T_CONST && $token[1] == 'const') {
                $const = true;
                $name = '';
            } else if ($token[0] == T_STRING && $const) {
                $const = false;
                $name = $token[1];
            } else if ($token[0] == T_CONSTANT_ENCAPSED_STRING && $name) {
                $constants[$name] = $token[1];
                $name = '';
            }
        }
    } else if ($token != '=') {
        $const = false;
        $name = '';
    }
}

foreach ($constants as $constant => $value) {
    echo "$constant = $value\n";
}
?>

Output:

LABEL_FIRST_NAME = "First Name"
LABEL_LAST_NAME = "Last Name"
LABEL_COMPANY_NAME = "Company"

In PHP5 you can use Reflection: (manual reference)

$class = new ReflectionClass('Profile');
$consts = $class->getConstants();

Per the PHP docs comments, if you're able to use the ReflectionClass (PHP 5):

function GetClassConstants($sClassName) {
    $oClass = new ReflectionClass($sClassName);
    return $oClass->getConstants();
}

Source is here.