【发布时间】:2015-04-07 08:00:42
【问题描述】:
如何在该类中定义的函数中访问类常量?
class Test{
const STATUS_ENABLED = 'Enabled';
const STATUS_DISABLED = 'Disabled';
public function someMethod(){
//how can I access ALL the constants here let's say in a form of array
}
}
我并不是要访问每个常量,而是以数组的形式访问所有常量。我看到了类似的东西:
<?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());
但是如果我想访问该类中的所有常量列表呢?
【问题讨论】:
-
好的,你已经编辑了你的问题,因为我们中的一些人已经提到了 Reflection 的 getConstants() 方法,说你已经在使用 getConstants() ......所以解释一下你有什么问题重新使用它,因为它看起来完全符合您的要求 - Demo。