8

Example class:

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 class static member variables without creating it manually like in the example?

1 Answer 1

10

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.