2009-06-05 88 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 
) 
相關問題