问题
I was working with a class where almost 20 constant are defined, as i want all these constant value in an array, i just want to know
is there any method which create an array of all constant of a class?
I tried with compact BUT it does not work with constants.
class Alpha
{
const ONE = 'fixone';
const TWO = 'fix_two';
const THREE = 3
public function __construct()
{
protected $arr_constant = compact(ONE,TWO,THREE); // gives FATAL Error
// is there any method which collect all consant and create an array?
protected $arr_contact = get_all_constant(__CLASS__);
var_dump($arr_constant);
}
}
回答1:
$ref = new ReflectionClass('Alpha');
var_dump($ref->getConstants());
回答2:
Use: http://php.net/manual/en/function.get-defined-constants.php
And: http://php.net/manual/en/reflectionclass.getconstants.php
来源:https://stackoverflow.com/questions/12174971/create-an-array-of-all-constant-of-a-class