DEV Community

Cover image for PHP Reflection
Matt Sparks
Matt Sparks

Posted on

PHP Reflection

Originally posted on DevelopmentMatt.

Beginning work on the Analyze PHP framework, specifically the container, brought reflection to my awareness. Before that I had maybe heard the term, but I definitely hadn't used it intentionally. Although it sounds like a scary computer science concept, it's not. It's actually quite simple:

Reflection is the ability of a computer program to examine, introspect, and modify its own structure...

That's it. (Thanks, Wikipedia)

Reflection in PHP

PHP provides a complete reflection API and multiple functions right out of the box. This provides an object the ability to examine itself and report back things like its methods and properties. With that functionality, we can automatically create documentation, inject dependencies, and a lot more.

It's possible that you have used reflection and didn't realize it. If you've ever used the get_class() or method_exists() functions, you've used reflection.
Examples of PHP Reflection

Here's the class we'll be working with:

<?php
namespace Springfield\Characters;

class Simpson
{
    private $name;

    public function __construct(string $name)
    {
        $this->name = $name;
    }

    public function getName() : string
    {
        return $this->name;
    }
}
Enter fullscreen mode Exit fullscreen mode

To start off, let's use some of PHP's built in functions to print out information about the class. First, we'll print out the class methods.

$homer = new Simpson('Homer');

var_dump(get_class_methods($homer));

// array(2) { [0]=> string(11) "__construct" [1]=> string(7) "getName" }
Enter fullscreen mode Exit fullscreen mode

Next, let's just get the name of the class.

$homer = new Simpson('Homer');

var_dump(get_class($homer));

// Springfield\Characters\Simpson
Enter fullscreen mode Exit fullscreen mode

As I mentioned above, PHP provides a complete API. The PHP Reflection class provides us with a lot of powerful tools to inspect our objects. Using the class is simple, just instantiate the class passing along the object you'd like to inspect. Like so:

$reflection = new \ReflectionClass($homer);
Enter fullscreen mode Exit fullscreen mode

Now you can get the class' properties:

var_dump($reflection->getProperties());

// array(1) { [0]=> object(ReflectionProperty)#3 (2) { ["name"]=> string(4) "name" ["class"]=> string(30) "Springfield\Characters\Simpson"  }
Enter fullscreen mode Exit fullscreen mode

Or get the constructor and learn about its parameters:

$constructor = $reflection->getConstructor();

var_dump($constructor->getParameters());

// array(1) { [0]=> object(ReflectionParameter)#4 (1) { ["name"]=> string(4) "name" } }
Enter fullscreen mode Exit fullscreen mode

Conclusion

While you may not use reflection daily, there are times it can come in very handy – working with other people's code, for example. I encourage you to checkout the links in the Further Reading section below to learn more.

Further Reading

Reflection (PHP Docs)
What is Reflection in PHP?
Introduction to PHP Reflection API

Top comments (0)