chicharito
chicharito

Reputation: 1087

How to access all constant variables of a class

I have a class

class iAmConstantClass
{
    const const1 = 'P';
    const const2 = 'T';
}

Now i need to validate a variable whose possible values can be any values that exists in class.

So can is there way by which i can loop all variables of a class i.e i can obtain value 'P' and 'T' without knowing variable name const1 and const2.

Upvotes: 0

Views: 62

Answers (1)

Arthur Halma
Arthur Halma

Reputation: 4001

You can use Reflection class for your needs:

$refl = new ReflectionClass('iAmConstantClass');
foreach($refl->getConstants() as $const){
  echo $const; // output will be PT
}

Upvotes: 4

Related Questions