Reflection on Reflection

Way too much meta!

Posted on January 18, 2014

Reflection is a very powerful concept in programming domain. It is widely harnessed for various purposes too. You must have noticed quite a few important application of reflection while programming, using frameworks, using IDEs etc. Infact, there are more than few places where we can use reflection for some good. Reflection as per wikipedia, is

the ability of a computer program to examine (see type introspection) and modify the structure and behavior (specifically the values, meta-data, properties and functions) of an object at runtime.

It is a fairly straightforward definition and easy to understand. Let us have a look at some applications of reflection that could give us some hint about the ubiquity and power of reflection in modern languages.

You must have faced some situation where you have to make some decisions during run time and then take appropriate actions. You might use reflection for achieving the same. For example, we have a class InputProcesser which processes the input for us and has following structure.

class InputProcesser {
    public function processString($input) {

    public function processInteger($input) {

It has two functions defined, one for string processing and other for integer processing. We can not identify the input datatype until run time. In order to call the apt method at run time, we might use reflection as following.

if (is_int($input)) {
    $methodName = 'processInteger';
} else {
    $methodName = 'processString';

$method = new ReflectionMethod('InputProcesser', $methodName);
$method->invoke(new InputProcesser(), $input);

or some people prefer other common way using call_user_func. Either way, they represent the idea of reflection.

If you use some IDE who does pretty awesome stuff based on the annotations or comments, you can again see reflection being used. For example, I use type hinting in PHPStorm to tell the IDE about the type of variable I am using

/** @var $processer InputProcesser */

like $processer is an instance of InputProcesser so that IDE can make my life with features like auto complete etc. This is just another example of annotation usage where metadata is read from the file using reflection and in some cases, it is part of program execution. Data provider in PHPUnit is a fine example of it, PHPDoc is another one in the list and you will find many more.

You can in fact operate on your code like a doctor using reflection or in other words you change your program using reflection. For example, you may alter the visibility of class members(for testing a piece of code or applying some hack).

private function cantCallMe() {

You can not call private method from outside class scope but you can change the accessibility of this method like this

$methodName = 'cantCallMe';

$method = new ReflectionMethod('InputProcesser', $methodName);

You can also analyze code for various purposes. Many frameworks who expect user defined classes to follow their interfaces might use reflection to verify the desired behaviour. XUnit frameworks analyzes classes for creating their mocks. Static code analyzing tools like pDepend also do the same.

In fact, there are also lots of cool things that you can experiment with reflection.

Although, reflection is a useful concept but you should not mindlessly start changing the program behaviour at run time. Changing program behaviour is a kind of dirty hack, instead of doing that you need to dig deep and find why you need to change it. It might take you to some design flaw at a certain level. Reflection should not be the first choice if you can do without it. Using reflection is slower than normal program execution because of reading and analyzing code. So, use it where it makes sense.