Reflection in PHP

Reflection is generally defined as a program's ability to inspect itself and modify its logic at execution time. In less technical terms, reflection is asking an object to tell you about its properties and methods, and altering those members (even private ones). In this lesson, we'll dig into how this is accomplished, and when it might prove useful.


A Little History

At the dawn of the age of programming, there was the assembly language. A program written in assembly resides on physical registers inside the computer. Its composition, methods and values could be inspected at any time by reading the registers. Even more, you could alter the program while it was running by simply modifying those registers. It required some intimate knowledge about the running program, but it was inherently reflective.

As with any cool toy, use reflection, but don't abuse it.

As higher-level programming languages (like C) came along, this reflectivity faded and disappeared. It was later re-introduced with object-oriented programming.

Today, most programming languages can use reflection. Statically typed languages, such as Java, have little to no problems with reflection. What I find interesting, however, is that any dynamically-typed language (like PHP or Ruby) is heavily based on reflection. Without the concept of reflection, duck-typing would most likely be impossible to implement. When you send one object to another (a parameter, for example), the receiving object has no way of knowing the structure and type of that object. All it can do is use reflection to identify the methods that can and cannot be called on the received object.


A Simple Example

Reflection is prevalent in PHP. In fact, there are several situations when you may use it without even knowing it. For example:

And:

In this code, we have a direct call to a locally initialized variable with a known type. Creating the editor in publishNextArticle() makes it obvious that the $editor variable is of type Editor. No reflection is needed here, but let's introduce a new class, called Manager:

Next, modify Nettuts, like so:

Now, Nettuts has absolutely no relation to the Editor class. It does not include its file, it does not initialize its class and it does not even know it exists. I could pass an object of any type into the publishNextArticle() method and the code would work.

Class Diagram

As you can see from this class diagram, Nettuts only has a direct relationship to Manager. Manager creates it, and therefore, Manager depends on Nettuts. But Nettuts no longer has any relation to the Editor class, and Editor is only related to Manager.

At runtime, Nettuts uses an Editor object, thus the <<uses>> and the question mark. At runtime, PHP inspects the received object and verifies that it implements the setNextArticle() and publish() methods.

Object Member Information

We can make PHP display the details of an object. Let's create a PHPUnit test to help us easily exercise our code:

Now, add a var_dump() to Nettuts:

Run the test, and watch the magic happen in the output:

Our reflection class has a name property set to the original type of the $editor variable: Editor, but that's not much information. What about Editor's methods?

In this code, we assign the reflection class' instance to the $reflector variable so that we can now trigger its methods. ReflectionClass exposes a large set of methods that you can use to obtain an object's information. One of these methods is getMethods(), which returns an array containing each method's information.

Another method, getProperties(), retrieves the properties (even private properties!) of the object:

The elements in the arrays returned from getMethod() and getProperties() are of type ReflectionMethod and ReflectionProperty, respectively; these objects are quite useful:

Here, we use getMethod() to retrieve a single method with the name of "publish"; the result of which is a ReflectionMethod object. Then, we call the invoke() method, passing it the $editor object, in order to execute the editor's publish() method a second time.

This process was simple in our case, because we already had an Editor object to pass to invoke(). We may have several Editor objects in some circumstances, giving us the luxury of choosing which object to use. In other circumstances, we may have no objects to work with, in which case we would need to obtain one from ReflectionClass.

Let's modify Editor's publish() method to demonstrate the double call:

And the new output:

Manipulating Instance Data

We can also modify code at execution time. What about modifying a private variable that has no public setter? Let's add a method to Editor that retrieves the editor's name:

This new method is called, getEditorName(), and simply returns the value from the private $name variable. The $name variable is set at creation time, and we have no public methods that let us change it. But we can access this variable using reflection. You might first try the more obvious approach:

Even though this outputs the value at the var_dump() line, it throws an error when trying to retrieve the value with reflection:

In order to fix this problem, we need to ask the ReflectionProperty object to grant us access to the private variables and methods:

Calling setAccessible() and passing true does the trick:

As you can see, we've managed to read private variable. The first line of output is from the object's own getEditorName() method, and the second comes from reflection. But what about changing a private variable's value? Use the setValue() method:

And that's it. This code changes "John Doe" to "Mark Twain".


Indirect Reflection Use

Some of PHP's built-in functionality indirectly uses reflection—one being the call_user_func() function.

The Callback

The call_user_func() function accepts an array: the first element pointing to an object, and the second a method's name. You can supply an optional parameter, which is then passed to the called method. For example:

The following output demonstrates that the code retrieves the proper value:

Using a Variable's Value

Another example of indirect reflection is calling a method by the value contained within a variable, as opposed to directly calling it. For example:

This code produces the same output as the previous example. PHP simply replaces the variable with the string it represents and calls the method. It even works when you want to create objects by using variables for class names.


When Should We Use Reflection?

Now that we've put the technical details behind us, when should we leverage reflection? Here are a few scenarios:

  • Dynamic typing is probably impossible without reflection.
  • Aspect Oriented Programming listens from method calls and places code around methods, all accomplished with reflection.
  • PHPUnit relies heavily on reflection, as do other mocking frameworks.
  • Web frameworks in general use reflection for different purposes. Some use it to initialize models, constructing objects for views and more. Laravel makes heavy use of reflection to inject dependencies.
  • Metaprogramming, like our last example, is hidden reflection.
  • Code analysis frameworks use reflection to understand your code.

Final Thoughts

As with any cool toy, use reflection, but don't abuse it. Reflection is costly when you inspect many objects, and it has the potential to complicate your project's architecture and design. I recommend that you make use of it only when it actually gives you an advantage, or when you have no other viable option.

Personally, I've only used reflection in a few instances, most commonly when using third party modules that lack documentation. I find myself frequently using code similar to the last example. It's easy to call the proper method, when your MVC responds with a variable containing "add" or "remove" values.

Thanks for reading!

Tags:

Comments

Related Articles