Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

What are Reflections in PHP?

Wednesday, November 30th, 2016 by Servage

php2Reflections is an advanced and fairly rare topic in PHP. Reflections allow you to inspect objects at runtime, and many frameworks depend on them to do their magic in the background. Even though reflections are not something you think of every day, you are likely using them without knowing it. Let us explore some of the most common reflection functions that allow you to inspect your code when it is already running.

Introduction to Reflections

Let us first define reflections in a little more detail. Reflections is a feature introduced in PHP 5 that allows you to reverse engineer classes, functions and more at runtime. It allows the PHP interpreter to learn about your code just like you learn about someone else’s code by reading it. Many frameworks rely on reflection. For example if you use dependency injection in Laravel, you can create a new instance of many objects without having to define them first. In the following example a Guzzle HTTP client is made available to a function using dependency injection:

public function getUrlContents(Client $client, $url)

In this case, you don’t have to specify how to construct the $client object because the framework can figure it out using reflections.

Reflection functions

Let’s have a look at some functions that you can use to inspect your own code at runtime. The first function is a simple one: get_class(). As the name suggests, it allows you to check a variable and see what class it has been made from. If you have created a variable $user from a class named User, calling get_class($user) returns “User”.

Sometimes you need to know what methods are part of an object. If you use an IDE, it is often smart enough to figure it out and tell you using intelligent code completion where you see a list of available methods in a dropdown menu. You can do the same with reflections at runtime. Have a look at the following code:

var_dump(get_class_methods($user))

The get_class_methods() function returns an array containing a list of all available methods for you to call. We are using var_dump() to dump the whole array to the page, but if you only want a specific method, you can use something like this: get_class_methods($user)[0]. This only returns the name of the first available method.

If the object your are inspecting inherits another class, get_class() returns the name of the child class. To get the name of the parent class, use get_parent_class() instead. The usage is identical to get_class(). To check whether an object is a subclass of some specific class, you can use the is_subclass_of() function. It works like this:

is_subclass_of($admin, "User")

If the $admin object inherits a class called “User”, this example would return true and false if it does not.

This time we only scratched the surface of reflections in PHP. Reflections is a big subject, and if you want to know more about it, the PHP documentation is a good starting point. They are definitely powerful and useful mechanism, and using it can greatly improve your codes versatility.

What are Reflections in PHP?, 4.0 out of 5 based on 3 ratings
Categories: Tips & Tricks

Keywords:

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.