Back to Top

What is Reflection in PHP

WHAT IS REFLECTION IN PHP

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.

  1. ReflactionClass
  2. ReflectionObject
  3. ReflectionMethod
  4. ReflectionProperty
  5. 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:

The following code shows a simple example of using the ReflectionClass

Example

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:

Example:

ReflectionProperty

Class ReflectionProperty allows to recover the original information of class properties.

Example:

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

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)

  1. Thanks for sharing this, Bhumi ! Thumbs UP!

  2. very very good tutorial
    thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

New Features in WordPress 3.5

Posted on 11 years ago

Bhumi

How to use HTML5 Page Visibility API

Posted on 9 years ago

Bhumi