Back to Top

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.


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:


$reflectionObj = new ReflectionClass('class_name');

The following code shows a simple example of using the ReflectionClass


$reflectionObj = new ReflectionClass("Imagick");

echo "Class name: ".$reflectionObj->getName()."
"; echo "Class new Instance: ".print_r($reflectionObj->newInstanceArgs())."
"; echo "Contants: ".print_r($reflectionObj->getConstants())."
"; echo "Constructor: ".print_r($reflectionObj->getConstructor())."
"; echo "Comment:".print_r($reflectionObj->getDocComment())."
"; echo "Properties: ".print_r($reflectionObj->getDefaultProperties())."
"; echo "Class Method: ".print_r($reflectionObj->getMethods())."

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.


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.


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.


$reflectionObj = new ReflectionFunction('function_name');


$reflectionObj = (new ReflectionFunction('str_replace'));
echo $reflectionObj->getStartLine();


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


class MyClass
  public $var = 1;
  protected $var1 = 2;
$reflectionObj = new ReflectionProperty('MyClass', 'var1');

In this, the first parameter is the name of the class or class object and the second parameter is the name of the property.


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

$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.


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

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

How to use MySQL Event Scheduler

Posted on 2 years ago


How to Accordion Menu with Modx

Posted on 6 years ago


How do I add items in Top navigation bar?

Posted on 5 years ago


Branching and Merging in Git

Posted on 1 year ago


Hello world

Updated 1 month ago

Nick Carter

In depth Understanding of Laravel Core

Updated 3 months ago


ECMAScript 6 New Features – 2

Updated 1 year ago


The Reader’s Poll – June 2015

Posted on 4 years ago


The Reader’s Poll – August 2014

Posted on 5 years ago


The Reader’s Poll – June 2014

Posted on 5 years ago


The Readers’ Poll – May 2014

Posted on 5 years ago


The Readers’ Poll – August 2013

Posted on 6 years ago