Get an array of static class member variables

Class Example:

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 ]; } } 

Is there a way in PHP to get an array of static member variables of a class without manually creating it, as in the example?

+8
php
source share
1 answer

Yes there is:

Using Reflection and the getStaticProperties () Method

 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

+10
source share

All Articles