Show How to analyze PHP data structure with PHPIntroductionWhen I started PHP coding, I wasn’t aware of the power of Reflection API and the main reason is that I didn’t need it to design my simple class, module or even my package, then I started to find it in many areas playing a major role. So in this part we will introduce Reflection API and here is the article agenda:
1. What is Reflection API
The point that you can stand on and look into your code (reverse-engineering), what does it mean? let us check the following snippet of code: /** Now, the // instantiation So ReflectionClass is like an analyst for our PHP is giving you the key to any locked box, so we have the keys for the following:
Check the full list on php.net. 2. Installation & Configuration:In order to use different Reflection API’s classes, there is no installation or configuration required, it is coming as a part of the core. 3. UsageHere, we have some examples how can we use Reflection API: ex.1) Get the parent class for a specific class: // here we have the child class ex.2) Get $method = new ReflectionMethod('Profile', 'getUserName'); ex.3) It can act like $class = new ReflectionClass('Profile'); ex.4) In some situations you will find yourself stuck with unit testing and wondering, how can I test the private functions?! No worries, here is the trick: // make getUserName() scope as private The preceding examples are quite simple, here are some areas you can find Reflection API extensively:
4. ConclusionPHP is providing a rich Reflection API that makes life easy to reach any area of the OOP structures. PHP is giving you the tools to fix a box… and it tells you how to look inside it. 5. References
|