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

前端之家收集整理的这篇文章主要介绍了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
       ];
   }       
}

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

就在这里:

使用ReflectiongetStaticProperties()方法

class Example{
   public static $ONE = [1,'three'];

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

var_dump(Example::test());

@L_502_2@

原文链接:https://www.f2er.com/php/137621.html

猜你在找的PHP相关文章