2009-06-05 86 views
109

我在一些类上定义了几个CONST,并且想要得到它们的列表。例如:我可以在PHP类上定义CONST吗?

class Profile { 
    const LABEL_FIRST_NAME = "First Name"; 
    const LABEL_LAST_NAME = "Last Name"; 
    const LABEL_COMPANY_NAME = "Company"; 
} 

有没有什么办法让在Profile类中定义的CONST的名单?据我所知,最接近的选项(get_defined_constants())不会做到这一点。

我真正需要的是不断的名单 - 是这样的:

array('LABEL_FIRST_NAME', 
    'LABEL_LAST_NAME', 
    'LABEL_COMPANY_NAME') 

或者:

array('Profile::LABEL_FIRST_NAME', 
    'Profile::LABEL_LAST_NAME', 
    'Profile::LABEL_COMPANY_NAME') 

甚至:

array('Profile::LABEL_FIRST_NAME'=>'First Name', 
    'Profile::LABEL_LAST_NAME'=>'Last Name', 
    'Profile::LABEL_COMPANY_NAME'=>'Company') 
+0

使用反射,以及CL A ReflectionClass,你可以使用函数getConstants http://nz.php.net/manual /en/class.reflectionclass.php – 2009-12-09 09:54:42

+0

[反射将是你的救星](http://se2.php.net/manual/en/reflectionclass.getconstants.php)。 – 2009-12-09 09:55:52

+0

您可以使用[反射](http://nz.php.net/oop5.reflection)执行此操作。在该页面上搜索“打印类常量”以查看示例。 – n3rd 2009-06-05 15:16:07

回答

191

您可以使用为了这。请注意,如果你正在做这么多,你可能想要缓存结果。

<?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()); 

输出:

Array 
(
    ['LABEL_FIRST_NAME'] => First Name 
    ['LABEL_LAST_NAME'] => Last Name 
    ['LABEL_COMPANY_NAME'] => Company 
) 
+4

两个小NB:第一,在5.3中,`Profile`可以用作反射器构造函数的参数,不带引号(简单的类名)。第二,要完全清楚,结果数组的键是字符串,而不是常量,因为这里的格式可能会被用来提示。 (值得一提的是fn是[无证](http://www.php.net/manual/en/reflectionclass.getconstants.php)。) – 2011-05-16 23:08:02

+11

@Benji XVI在5.3中,如果你打开了通知,不用引号就可以使用`Profile`,因为它会显示以下错误:注意:使用未定义的常量Profile - 假定为'Profile'。所以我建议保留引号``Profile`` – toneplex 2011-06-28 15:28:44

4

呀,你用reflection。看看输出

<? 
Reflection::export(new ReflectionClass('YourClass')); 
?> 

这应该让你知道你会看到什么。

14

使用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"; 
} 
?> 

输出:

LABEL_FIRST_NAME = "First Name" 
LABEL_LAST_NAME = "Last Name" 
LABEL_COMPANY_NAME = "Company" 
7

使用ReflectionClass和getConstants()给你想要什么:

<?php 
class Cl { 
    const AAA = 1; 
    const BBB = 2; 
} 
$r = new ReflectionClass('Cl'); 
print_r($r->getConstants()); 

输出:

Array 
(
    [AAA] => 1 
    [BBB] => 2 
) 
12

每PHP的文档评论,如果你是能够使用ReflectionClass(PHP 5):

function GetClassConstants($sClassName) { 
    $oClass = new ReflectionClass($sClassName); 
    return $oClass->getConstants(); 
} 

Source is here.

2

为什么不把它们放在一类变量,数组开始?使循环更容易。

private $_data = array("production"=>0 ...); 
18

This

$reflector = new ReflectionClass('Status'); 
var_dump($reflector->getConstants()); 
2
与命名空间

最终:

namespaces enums; 
class enumCountries 
{ 
    const CountryAustria   = 1 ; 
    const CountrySweden   = 24; 
    const CountryUnitedKingdom = 25; 
} 

namespace Helpers; 
class Helpers 
{ 
    static function getCountries() 
    { 
    $c = new \ReflectionClass('\enums\enumCountries'); 
    return $c->getConstants(); 
    } 
} 

print_r(\Helpers\Helpers::getCountries()); 
3

在类中有一个方法返回它自己的常量是很方便的。
你可以这样说:

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()); 
0

特质与静态方法 - 救援

看起来它是用性状与静态函数来扩展类的功能的好地方。 Traits也可以让我们在任何其他类中实现这个功能,而不会一遍又一遍地重写相同的代码(保持干燥)。

在Profile类中使用我们自定义的'ConstantExport'Trait。为每个需要此功能的课程做好准备。

/** 
* 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; 

} 

应用例

// So simple and so clean 
$constList = Profile::getConstants(); 

print_r($constList); // TEST 

输出:

Array 
(
    [LABEL_FIRST_NAME] => First Name 
    [LABEL_LAST_NAME] => Last Name 
    [LABEL_COMPANY_NAME] => Company 
) 
相关问题