Reflection In PHP

Object orientated programming in PHP many times is confined to classes which acts as a DataAccessLayer for the uderlyoing database, sort of beans in java.
Going a step further , we could have our own DataAccessLayer for the CRUD operations. (*CRUD – Create , Read , Update and Delete).

While creating functions for CRUD operations, they would normally take in the object. So, it turns out that our DAL should be able to get the classname, the methods and members of a class, given an object. For this ‘Reflection’ is used.

In object oriented programing languages such as Java, reflection allows inspection of classes, interfaces, fields and methods at runtime without knowing the names of the interfaces, fields, methods at compile time. It also allows instantiation of new objects and invocation of methods.

In PHP, we make use of the “The ReflectionClass “, which reports information about a class.
A detailed look at this class could be found at

    *  <p>This private function is used to get all methods of a class using reflection
   private function getClassMethods($classname)
       $methods = array();

       if (!class_exists($classname))
           throw new Exception ("Class $classname does not exists.");

       $reflector = new ReflectionClass($classname);
       $reflected_methods = $reflector->getMethods();

       foreach ($reflected_methods as $reflected) {
           $methods[] = $reflected->getName();
       return $methods;

Another function below takes in an Object as the input and gives away the classname of that object.

    * This private function takes object as input and gives away class name as output
   private function getClass($instance)
       $reflector = new ReflectionObject($instance);
       if (!$reflector->isInstance($instance))
           return false;
       return $reflector->getName();

These in many scenarios would ease our work, wherein we get an object and without bothering about more details, use the reflection to take out all the information that is needed.

You could ask any PHP OOPs related clarifications here 🙂
Happy and easy PHP coding for all ….

About mytechlifedays

Its been a wonderful learning period over the last 6 years in the IT industry , getting exposed to whole lot of technologies and ideas. The hurdles and the crisis that came along have been wonderful experience ... And now its time to pen them down so that let some others execute faster and easily with these information .....
This entry was posted in PHP and tagged , , . Bookmark the permalink.

Leave a Reply ! It would be always appreciated ! :)

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s