2012-02-27 6 views
0

정적 메서드 내에서 클래스 상수 목록을 가져 오려고합니다.PHP : ReflectionClass를 사용하여 정적 메서드에서 클래스 상수 가져 오기

public static function example() 
{ 
    $reflection = new \ReflectionClass(get_called_class()); 
    var_dump($reflection -> getConstants()); 
} 

작동이에 대한 어떠한 방법이 있나요 Fatal error: Cannot access self:: when no class scope is active

을 예외, 또는 나는 PHP에서 다른 언어의 제한에 반대입니까?

답변

0

난 그냥 코드를 시도했습니다 그리고 당신이 실제 수업의 예제를 제공 할 수 잘 작동합니까?

class test23 { 
    const te = 'asd'; 
    var $ya = 'hoopla'; 
    public static function example() 
    { 
     $reflection = new ReflectionClass(get_called_class()); 
     var_dump($reflection -> getConstants()); 
    } 
} 

test23::example(); 반환

-2
// creates a reflection class object 
$reflection = new ReflectionClass ($this); 

//gets all the constants of the current class 
$consts = $reflection->getConstants(); 

array(1) { ["te"]=> string(3) "asd" }는 도움이되기를 바랍니다.

관련 문제