Reputation: 1087
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
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