2015-12-27 19 views
8

例クラスの配列を取得します。クラスの静的メンバ変数

class Example{ 
    public static $ONE = [1,'one']; 
    public static $TWO = [2,'two']; 
    public static $THREE = [3,'three']; 

    public static function test(){ 

     // manually created array 
     $arr = [ 
      self::$ONE, 
      self::$TWO, 
      self::$THREE 
     ]; 
    }  
} 

は、例のようにそれを手動で作成することなく、クラスの静的メンバ変数の配列を取得するPHPでの方法はありますか?

答えて

10

はいある:Reflectionを使用

、及びgetStaticProperties()方法

class Example{ 
    public static $ONE = [1,'one']; 
    public static $TWO = [2,'two']; 
    public static $THREE = [3,'three']; 

    public static function test(){ 
     $reflection = new ReflectionClass(get_class()); 
     return $reflection->getStaticProperties(); 
    }  
} 

var_dump(Example::test()); 

Demo

関連する問題