📜 ⬆️ ⬇️

Introduction to PHP Reflection API

Hi, Habr! I present to your attention the translation of the article " Introduction to PHP Reflection API " by Mustafa Magdi .

How to analyze data structure in PHP




Introduction


When I started programming in PHP, I did not know about the capabilities of the Reflection API . The main reason is that I did not need to design my simple classes, modules, or even packages. Then I discovered that it plays a major role in many areas. In the article we will discuss the Reflection API on the following points:
')
  1. What is Reflection API
  2. Installation and configuration
  3. Using
  4. Conclusion
  5. Recommendations


1. What is Reflection API


In computer science, reflection or reflection (holonim introspection, English reflection) means a process during which a program can monitor and modify its own structure and behavior at run time. - Wikipedia .
What does the ability to stop and take a look inside your code ( reverse-engineering ) mean? Let's look at the following code snippet:

/** * Class Profile */ class Profile { /** * @return string */ public function getUserName(): string { return 'Foo'; } } 

The Profile class is a black box. Using the Reflection API you can read what is inside:

 //  $reflectionClass = new ReflectionClass('Profile'); //    var_dump($reflectionClass->getName()); => output: string(7) "Profile" //    var_dump($reflectionClass->getDocComment()); => output: string(24) "/** * Class Profile */" 

Thus, ReflectionClass acts as an analyst for our Profile class, and this is the main idea of ​​the Reflection API .

PHP gives you the key to any locked box, so we have the keys
for the following:
ReflectionClass : reports class information.
ReflectionFunction : reports information about the function.
ReflectionParameter : retrieves information about the parameters of a function or method.
ReflectionClassConstant : reports information about a class constant.

You can learn a full list on php.net

2. Installation and configuration


There is no need to install or configure anything to use the Reflection API classes, as they are part of the PHP core.

3. Examples of use


Below are some examples of how we can use the Reflection API :

Example 1:
Get the parent class for a specific class:

 //   class Child extends Profile { } $class = new ReflectionClass('Child'); //     print_r($class->getParentClass()); // ['Profile'] 

Example 2:
Get the getUserName() method documentation:

 $method = new ReflectionMethod('Profile', 'getUserName'); var_dump($method->getDocComment()); => output: string(33) "/** * @return string */" 

Example 3:
It can be used as instanceOf and is_a() for validating objects:

 $class = new ReflectionClass('Profile'); $obj = new Profile(); var_dump($class->isInstance($obj)); // bool(true) //    var_dump(is_a($obj, 'Profile')); // bool(true) //    var_dump($obj instanceof Profile); // bool(true) 

Example 4:
In some situations, you can get stuck with unit testing and ask yourself: “How can I test a private function ?!”

Do not worry, here is the trick:

 //    getName() private function getName(): string { return 'Foo'; } $method = new ReflectionMethod('Profile', 'getUserName'); //          if ($method->isPrivate()) { $method->setAccessible(true); } echo $method->invoke(new Profile()); // Foo 

The previous examples are fairly simple, but there are other examples in which you can see how the Reflection API is used more extensively:
  • API Documentation Generator : The lavarel-apidoc-generator package makes extensive use of ReflectionClass and ReflrectionMethod to receive and subsequently process information about the documentation of the classes and methods, and design these blocks of code.
  • Dependency Injection Container : check the whole topic here.

4. Conclusion


PHP provides a complete Reflection API that helps you easily reach different areas of OOP structures.

5. References



From the translator:

You can also see an example of using the Reflection API in the Codeception package in the Stub class.
This class through reflection helps us to wash methods and properties in unit tests.

It should be added that the Reflection API is quite slow, so you should not get carried away. It is recommended to use in tests or during debugging, but if you can do without it, then it is better to do so. And it is absolutely not recommended to use it in the project work code, since it's still not safe.

Source: https://habr.com/ru/post/433266/


All Articles