PHP Reflection Class

Miqayel Srapionyan
4 min readMar 18, 2023

--

What is Reflection class in PHP?

The reflection class adds the ability to introspect classes, interfaces, functions, methods, and extensions. It also offers ways to retrieve doc comments for functions, classes, and methods. The reflection class is used to get information about the current state of the application. It’s called reflection, because it looks at itself, and can tell you information about the program you run, at run time.

Installation?

No external libraries are needed to build this extension.
There is no installation needed to use these functions; they are part of the PHP core.

There are many Reflection Classes for different purposes.

For example:

  • The ReflectionClass class reports information about a class.
  • The ReflectionClassConstant class reports information about a class constant.
  • The ReflectionEnum class reports information about an Enum.
  • The ReflectionExtension class reports information about an extension.
  • The ReflectionFunction class reports information about a function.
  • The ReflectionMethod class reports information about a method.
  • The ReflectionObject class reports information about an object.
  • The ReflectionParameter class retrieves information about the function’s or method’s parameters
  • The ReflectionProperty class reports information about class properties.
  • The ReflectionGenerator class reports information about a generator.
  • The ReflectionAttribute class provides information about an Attribute.
  • And a few others…

Let's have a closer look at ReflectionClass

For example, you want to inspect class constants. To do so we can initialize ReflectionClass and pass our class to it.

<?php

use ReflectionClass;

class MyClass {
const NONE = 0;
const REQUEST = 100;
const AUTH = 101;

public function funcPublic(){}

protected function funcProtected(){}

private function funcPricate(){}
}

$myClassInstance = new MyClass();
$reflection = new ReflectionClass($myClassInstance); // OR MyClass::class as parameter
$reflection->getConstants(); // ['NONE' => 0, 'REQUEST' => 100, 'AUTH' => 101]

// OR get constant with name
$reflection->getConstant('NONE'); // 0

// Get methods
$reflection->getMethods();
/*
Array
(
[0] => ReflectionMethod Object
(
[name] => funcPublic
[class] => MyClass
)

[1] => ReflectionMethod Object
(
[name] => funcProtected
[class] => MyClass
)

[2] => ReflectionMethod Object
(
[name] => funcPricate
[class] => MyClass
)

)
*/

// We can get method information from ReflectionMethod Object
$privateMethod = $reflection->getMethod('funcPricate'); // get single method by name
$privateMethod->isPrivate(); // 1

ReflectionClass takes as a parameter string|object $objectOrClass Either a string containing the name of the class to reflect or an object.
It will throw ReflectionException if there will be some invalid data.

<?php

use ReflectionClass;

new ReflectionClass('NotExistingClass'); // Fatal error: Uncaught exception 'ReflectionException' with message 'Class NotExistingClass does not exist'
<?php

use ReflectionClass;

class MyClass {

}

$reflection = new ReflectionClass('MyClass');
$reflection->getMethod('funcPricate'); // Fatal error: Uncaught exception 'ReflectionException' with message 'Method funcPricate does not exist'

We can get docComments from class. Doc comments start with /**, followed by whitespace. If there are multiple doc comments above the class definition, the one closest to the class will be taken.

<?php

use ReflectionClass;

/**
* A super usfull class.
*
* @param foo bar
* @return baz
*/
class MyClass {

}

$reflection = new ReflectionClass('MyClass');
$reflection->getDocComment();
//string(74) "/**
// * A super usfull class.
// *
// * @param foo bar
// * @return baz
// */"

We can parse doc comments and get all the necessary information and write our logic.
We can get doc comments for methods and properties as well.

<?php

use ReflectionClass;

class MyClass {
/** @var string */
public $property1;

/** @var int*/
public $property2;

/** Something about function*/
public function myFunc(){}
}

$reflection = new ReflectionClass('MyClass');
$properties = $reflection->getProperties();
$methods = $reflection->getMethods();

foreach($properties as $property){
echo $property->getDocComment(); // /** @var string *//** @var int*/
}

foreach($methods as $method){
echo $method->getDocComment(); // /** Something about function*/
}

PHP 8 introduced Attributes, we can parse them as well.

<?php

#[Attribute]
class Fruit {
}

#[Attribute]
class Red {
}

#[Fruit]
#[Red]
class Apple {
}

$class = new ReflectionClass('Apple');
$attributes = $class->getAttributes();
print_r(array_map(fn($attribute) => $attribute->getName(), $attributes));.
/**
Array
(
[0] => Fruit
[1] => Red
)
*/

We can get a class constructor as well. This is super useful when creating Dpenedency Injection Containers.

<?php

use ReflectionClass;

class InjectableClass {

}

class MyClass {
protected InjectableClass $injectableClass;

public function __construct(InjectableClass $injectableClass){
$this->injectableClass = $injectableClass;
}
}

$reflection = new ReflectionClass('MyClass');
$constructor = $reflection->getConstructor(); // The type of $constructor is ReflectionMethod
$parameters = $constructor->getParameters(); // The type of parameters is ReflectionParameter

$name = $parameters[0]->getName(); // injectableClass
$type = $parameters[0]->getType(); // ReflectionNamedType
$type->getName(); // InjectableClass
$type->isBuiltin(); // false

You can traverse on parameters and do your logic for DI, but it is out of scope for this topic.

Conclusion

Reflection has lots of abilities, I just mention a few of them. I want you to know about it, and what it can do.
There is no magic :)

--

--

Miqayel Srapionyan

Software Engineer with a passion to learn technologies and share in stories.