create an array of all constant of a class?

孤街浪徒 提交于 2020-02-01 05:16:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!