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 in PHP to analyze data structure 3r3133.
 
Introduction to PHP Reflection API
 
 
3r33333. Entry
 
When I started programming in PHP, I didn’t know about the possibilities of 3r33360. Reflection API [/b] . 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 look at Reflection API under the following points:
 
 
 
3r33352. What is Reflection API 3r33333.  
3r33352. Install and configure 3r33333.  
3r33352. Using 3r33333.  
3r33352. Conclusion 3r33333.  
3r33352. Recommendations 3r33333.  
3r362.
 
3r3365.
 
php.net
 
 
3r? 3175.
3r33333. 2. Installing and configuring
 
For use classes Reflection API There is no need to install or configure anything, as they are part of the PHP core.
 
 
3r3187.
3r33333. 3. Examples of the use of 3r33342.
 
Below are a few examples of how we can use r3r3360. Reflection API [/b] :
 
 
Example 1:
 
Get the parent class for a specific class:
 
 
//child class
class Child extends Profile
{
}
$ class = new ReflectionClass ('Child');
//get the list of all parents
print_r ($ class-> getParentClass ()); //['Profile']

 
Example 2:
 
Get the documentation of method getUserName () :
 

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

 
Example 3:
 
Can be used as 3r33250. instanceOf and is_a () for validating objects:
 

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

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

 
Do not worry, here is a trick:
 

 
    //add a private method getName ()
private function getName (): string
{
return 'Foo';
}
$ method = new ReflectionMethod ('Profile', 'getUserName');
//check if the method is closed and make it available
if ($ method-> isPrivate ()) {
$ method-> setAccessible (true);
}
echo $ method-> invoke (new Profile ()); //Foo

 
The previous examples are pretty simple, but there are other examples in which you can see Reflection API used more extensively:
 
3r3302. 3r33333.  
3r33352. 3r33360. API documentation generator [/b] : package 3r3308. lavarel-apidoc-generator
widely used ReflectionClass and ReflrectionMethod for obtaining and subsequent processing of information about the documentation of classes and methods, and design of these blocks of code. 3r33333.  
3r33352. 3r33360. Dependency Injection Container [/b] : check the whole topic you can here 3r33333.  
3r33357. 3r3323.
 
Official Reflection API documentation 3r33333.  
3r33352. Article about Dependency Injection Container 3r33333.  
3r33357.
 
3r33360. From the translator: [/b]
 
 
You can also see an example of using the Reflection API in the Codeception package in the class Stub .
 
This class through reflection helps
mocak
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 it 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 working code of the project, it's still not safe.
3r33385.
! function (e) {function t (t, n) {if (! (n in e)) {for (var r, a = e.document, i = a.scripts, o = i.length; o-- ;) if (-1! == i[o].src.indexOf (t)) {r = i[o]; break} if (! r) {r = a.createElement ("script"), r.type = "text /jаvascript", r.async =! ? r.defer =! ? r.src = t, r.charset = "UTF-8"; var d = function () {var e = a.getElementsByTagName ("script")[0]; e.parentNode.insertBefore (r, e)}; "[object Opera]" == e.opera? a.addEventListener? a.addEventListener ("DOMContentLoaded", d,! 1): e.attachEvent ("onload", d ): d ()}}} t ("//mediator.mail.ru/script/2820404/"""_mediator") () (); 3r33383.
3r33385.
+ 0 -

Add comment