创建一个类的所有常量数组? [英] create an array of all constant of a class?

查看:197
本文介绍了创建一个类的所有常量数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跟这里几乎20常数定义的类的工作,因为我想在一个数组中所有的恒定值,我只是想知道

是有它创建所有不变的一类阵列的任何方法?

我试着用紧凑不过,这并不与常量工作。

 类阿尔法
{
 常量ONE ='fixone';
 常量两='fix_two';
 常量THREE = 3   公共职能__construct()
   {
     保护$ arr_constant =紧凑(一,二,三级); //给人致命错误
     //是有它收集所有consant和创建阵列的任何方法?
     保护$ arr_contact = get_all_constant(__ CLASS__);
     后续代码var_dump($ arr_constant);
   }
}


解决方案

  $ REF =新ReflectionClass('阿尔法');
的var_dump($ REF-> getConstants());

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);
   }
}

解决方案

$ref = new ReflectionClass('Alpha');
var_dump($ref->getConstants());

这篇关于创建一个类的所有常量数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆