David Jones

Reflection class to access a static property from a variable

The other day at work I need to grab the value of a static property based on a variable and I got a bit stuck until I used a reflection class.

The definition of a reflection class is as follows, adds the ability to reverse-engineer classes, interfaces, functions, methods and extensions. Additionally, the reflection API offers ways to retrieve doc comments for functions, classes and methods.

I used it in the following way.

$reflect = new ReflectionClass( 'Answers' );
$prop = $reflect->getStaticPropertyValue( $property_name );