Bro*_*and 130 php constants class-constants
我在一些类上定义了几个CONST,并希望得到它们的列表.例如:
class Profile {
const LABEL_FIRST_NAME = "First Name";
const LABEL_LAST_NAME = "Last Name";
const LABEL_COMPANY_NAME = "Company";
}
Run Code Online (Sandbox Code Playgroud)
有没有办法获得在Profile课堂上定义的CONST列表?据我所知,最接近的选项(get_defined_constants())不会起作用.
我真正需要的是一个常量名称列表 - 如下所示:
array('LABEL_FIRST_NAME',
'LABEL_LAST_NAME',
'LABEL_COMPANY_NAME')
Run Code Online (Sandbox Code Playgroud)
要么:
array('Profile::LABEL_FIRST_NAME',
'Profile::LABEL_LAST_NAME',
'Profile::LABEL_COMPANY_NAME')
Run Code Online (Sandbox Code Playgroud)
甚至:
array('Profile::LABEL_FIRST_NAME'=>'First Name',
'Profile::LABEL_LAST_NAME'=>'Last Name',
'Profile::LABEL_COMPANY_NAME'=>'Company')
Run Code Online (Sandbox Code Playgroud)
Tom*_*igh 232
您可以使用Reflection进行此操作.请注意,如果您正在执行此操作,则可能需要查看缓存结果.
<?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());
Run Code Online (Sandbox Code Playgroud)
输出:
Array
(
'LABEL_FIRST_NAME' => 'First Name',
'LABEL_LAST_NAME' => 'Last Name',
'LABEL_COMPANY_NAME' => 'Company'
)
Run Code Online (Sandbox Code Playgroud)
Wri*_*ken 21
$reflector = new ReflectionClass('Status');
var_dump($reflector->getConstants());
Run Code Online (Sandbox Code Playgroud)
cle*_*tus 15
使用token_get_all().即:
<?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";
}
?>
Run Code Online (Sandbox Code Playgroud)
输出:
LABEL_FIRST_NAME = "First Name"
LABEL_LAST_NAME = "Last Name"
LABEL_COMPANY_NAME = "Company"
Run Code Online (Sandbox Code Playgroud)
Par*_*ase 14
在PHP5中,您可以使用Reflection :( 手动参考)
$class = new ReflectionClass('Profile');
$consts = $class->getConstants();
Run Code Online (Sandbox Code Playgroud)
mwa*_*way 13
根据PHP文档注释,如果您能够使用ReflectionClass(PHP 5):
function GetClassConstants($sClassName) {
$oClass = new ReflectionClass($sClassName);
return $oClass->getConstants();
}
Run Code Online (Sandbox Code Playgroud)
使用ReflectionClass并getConstants()准确提供您想要的内容:
<?php
class Cl {
const AAA = 1;
const BBB = 2;
}
$r = new ReflectionClass('Cl');
print_r($r->getConstants());
Run Code Online (Sandbox Code Playgroud)
输出:
Array
(
[AAA] => 1
[BBB] => 2
)
Run Code Online (Sandbox Code Playgroud)
在类中有一个方法来返回它自己的常量是很方便的。
你可以这样做:
class Profile {
const LABEL_FIRST_NAME = "First Name";
const LABEL_LAST_NAME = "Last Name";
const LABEL_COMPANY_NAME = "Company";
public static function getAllConsts() {
return (new ReflectionClass(get_class()))->getConstants();
}
}
// test
print_r(Profile::getAllConsts());
Run Code Online (Sandbox Code Playgroud)
是的,您需要使用反射。看一下输出
<?
Reflection::export(new ReflectionClass('YourClass'));
?>
Run Code Online (Sandbox Code Playgroud)
那应该使您对所要看的东西有想法。
看起来这是将Traits与静态函数结合使用来扩展类功能的好地方。特性还将使我们能够在任何其他类中实现此功能,而无需一遍又一遍地重写相同的代码(保持DRY)。
在Profile类中使用我们的自定义'ConstantExport'特性。对需要此功能的每个类都这样做。
/**
* ConstantExport Trait implements getConstants() method which allows
* to return class constant as an assosiative array
*/
Trait ConstantExport
{
/**
* @return [const_name => 'value', ...]
*/
static function getConstants(){
$refl = new \ReflectionClass(__CLASS__);
return $refl->getConstants();
}
}
Class Profile
{
const LABEL_FIRST_NAME = "First Name";
const LABEL_LAST_NAME = "Last Name";
const LABEL_COMPANY_NAME = "Company";
use ConstantExport;
}
Run Code Online (Sandbox Code Playgroud)
使用范例
// So simple and so clean
$constList = Profile::getConstants();
print_r($constList); // TEST
Run Code Online (Sandbox Code Playgroud)
输出:
Array
(
[LABEL_FIRST_NAME] => First Name
[LABEL_LAST_NAME] => Last Name
[LABEL_COMPANY_NAME] => Company
)
Run Code Online (Sandbox Code Playgroud)