获取类静态成员变量的数组

Jam*_*May 8 php

示例类:

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
       ];
   }       
}
Run Code Online (Sandbox Code Playgroud)

有没有办法在PHP中获取类静态成员变量数组而不像示例中那样手动创建它?

Mar*_*ker 10

就在这里:

使用ReflectiongetStaticProperties()方法

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());
Run Code Online (Sandbox Code Playgroud)

演示