While you are working on the web application, sometimes it is necessary to fetch the information about the structure of a particular class and to get the information, you can use the Reflection.
What is Reflection in PHP?
PHP 5 introduced very useful Reflection API which is not much known to developers so Today we will take a brief overview of Reflection API in PHP. Reflection is the built-in class of PHP which allows us to retrieve all the information about classes, class properties, functions, methods, and exceptions. PHP supports reflection using the Reflection API in PHP5. Reflection is not needed for a vast majority of projects. Reflection API is very useful in cases you are developing the complex application.
Reflection is kind of observer which observer the script.Reflection is mainly useful for dynamically invoking methods that a class has and contains a number of different methods to obtain information about the structure of the class.
What is Reflection API?
Reflection API is an object-oriented extension of PHP. Reflection API provides a much larger classes and methods for solving problems of reflection.Reflection is very easy to use in your code.Reflection is useful when you are using OOP concept for your development as it works more effectively with object-oriented PHP.
Reflection API is only used for retrieving information about the classes. You can use ReflectionFunction class to get information about a given function, You can get information about extensions using ReflectionExtension class in PHP.
ReflectionClass provides information of the object directly when executing the script. For example, you can get information about a certain object, methods,their parameters including comment when run the script.
The reflection API contain many classes that you can use in your application.Let’s see the list of it.
- ReflactionClass
- ReflectionObject
- ReflectionMethod
- ReflectionProperty
- ReflectionExtension
we demonstrate the example to get an understanding how to use Reflection.
ReflactionClass
Class ReflectionClass allows you to retrieve the original information classes.the reflection is the virtual way to call a method whose name you do not know at compile time.
First of all, you need to create an object of class ReflectionClass and pass it the name of a class constructor, the information which is necessary to obtain in the future. Since we have created a test class Test, the name and pass:
SYNTAX:
1 2 3 | $reflectionObj = new ReflectionClass('class_name'); |
The following code shows a simple example of using the ReflectionClass
Example
1 2 3 4 5 6 7 8 9 10 11 | echo "Class name: ".$reflectionObj->getName()."<br/>"; echo "Class new Instance: ".print_r($reflectionObj->newInstanceArgs())."<br/>"; echo "Contants: ".print_r($reflectionObj->getConstants())."<br/>"; echo "Constructor: ".print_r($reflectionObj->getConstructor())."<br/>"; echo "Comment:".print_r($reflectionObj->getDocComment())."<br/>"; echo "Properties: ".print_r($reflectionObj->getDefaultProperties())."<br/>"; echo "Class Method: ".print_r($reflectionObj->getMethods())."<br/>"; |
First of all, Create an object of class ReflectionClass and using that object call the different function and get the information.
To restore information first class creates an instance of the class ReflectionClass. You can then call any of the above methods of the instance.Isn’t it very useful? You can check each and every information of class very easily.
ReflectionObject
Class ReflectionObject allows to recover the original information of objects. Most of the methods of the ReflectionObject is the same as that of the ReflectionClass. The Main difference is, You can obtain the properties that it contain the object and the class it is not.
ReflectionMethod
Class ReflectionMethod allows to recover the original information of class methods.
To understand the method, first creates an instance of the class Reflection Methods. You can then call any of the above methods of the instance.
SYNTAX:
1 2 3 | $reflectionObj = new ReflectionFunction('function_name'); |
Example:
1 2 3 4 5 | $reflectionObj = (new ReflectionFunction('str_replace')); echo $reflectionObj->getStartLine(); print_r($reflectionObj->getParameters()); |
ReflectionProperty
Class ReflectionProperty allows to recover the original information of class properties.
Example:
1 2 3 4 5 6 7 8 9 | class MyClass { public $var = 1; protected $var1 = 2; } $reflectionObj = new ReflectionProperty('MyClass', 'var1'); print_r($reflectionObj); |
In this, the first parameter is the name of the class or class object and the second parameter is the name of the property.
ReflectionExtension
Class ReflectionExtension allows you to recover the original information extensions. You can retrieve all loaded extensions at run-time using the script get_loaded_extensions ()
Let’s take a look how to use reflection to get extension information
1 2 3 4 | $reflect = new ReflectionExtension('ZipArchive'); echo $reflect; |
This will list out all the information about extension “ZipArchive” once you print it. it demonstrates how reflection helpful to us.The ReflectionExtension is a kind of reverse-engineer on extensions. You can retrieve all information about extensions at run-time.
Conclusion:
Reflection API is more powerful and gives you a rich set of tools to work directly with your application but the issue is to instantiate ReflectionClass or ReflectionProperty is very slow and unreadable.
That’s all I wanted to show you in this tutorial. We have tried to cover most options of Reflection in PHP. Reflection is very useful if you are creating a mechanism to automatically call from different classes. Now you can access any property from anywhere.
Comments (2)