PHP Reflection
Tags: php, webdev
Published: 2018-06-27 22:22:25

---
title: PHP Reflection
published: true
description: What is Reflection in PHP and how do you use it? This post breaks down what reflection is, when it's used, and gives examples.
tags: php, webdev
cover_image: http://developmentmatt.com/wp-content/uploads/2018/06/mirroring-711926_1280.jpg
---

*Originally posted on [DevelopmentMatt](https://developmentmatt.com/php-reflection/).*

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](https://en.wikipedia.org/wiki/Reflection_(computer_programming)))

## Reflection in PHP

PHP provides a complete [reflection API](http://php.net/manual/en/book.reflection.php) 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
<?php
namespace Springfield\Characters;

class Simpson
{
    private $name;

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

    public function getName() : string
    {
        return $this->name;
    }
}
```

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.

```php
$homer = new Simpson('Homer');

var_dump(get_class_methods($homer));

// array(2) { [0]=> string(11) "__construct" [1]=> string(7) "getName" }
```

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

```php
$homer = new Simpson('Homer');

var_dump(get_class($homer));

// Springfield\Characters\Simpson
```

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:

```php
$reflection = new \ReflectionClass($homer);
```

Now you can get the class' properties:

```php
var_dump($reflection->getProperties());

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

Or get the constructor and learn about its parameters:

```php
$constructor = $reflection->getConstructor();

var_dump($constructor->getParameters());

// array(1) { [0]=> object(ReflectionParameter)#4 (1) { ["name"]=> string(4) "name" } }
```

## 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)](http://php.net/manual/en/book.reflection.php)
[What is Reflection in PHP?](https://medium.com/tech-tajawal/introduction-to-php-reflection-api-4af07cc17db4)
[Introduction to PHP Reflection API](https://medium.com/tech-tajawal/introduction-to-php-reflection-api-4af07cc17db4)