Reflection Api in Php

Author - Gopi Chand
11.06.2018
|
0 Comments
|

Reflection Api in Php

What is Reflection and why is it useful?
Reflection is generally defined as a program’s ability to examine itself and modify its logic/methods at execution time. In simple terms, reflection is asking an object to tell you about its properties and methods, and altering those members. It is also referred to as “Introspection”.

Reflection image example:

This is useful in a number of different ways:

1. It is useful for debugging your code. You may have used the get_class() and get_class_methods() methods when working with ambiguous objects. The ability to get the type or functions of an object when you do not know what is the type of object is an example of Reflection.

2. It is used for creating documentation. It would be very hard to write documentation about every function of every class of a large framework or application. Instead, Reflection can automatically generate the documentation for your framework or application. It does this by examining each function, constructor, and class to determine what goes into parameters and what comes out as result.

PHP Reflection API Installation & Configuration:

For using PHP Reflection API classes, there is no installation or configuration required, it is already a part of the core.

Some common classes available in PHP Reflection API:

Some common methods Available in PHP Reflection API:

Benefits of using Reflection:

1. Duck typing is not possible with Reflection API.
2. HTML Form Generation with Reflection API.
3. Creating DB Tables
4. Creating Documentation.
5. Accessing private properties and methods.

Examples:

1. Using ‘ReflectionClass’ class to get class information:

 class myClass{
public $var;
public function myFun(){
echo "Hello";
}
}
$myClassObj = new myClass();
$myClassRef = new ReflectionClass($myClassObj);
echo "
";
print_r($myClassRef);
print_r($myClassRef->getProperties());
print_r($myClassRef->getMethods());

2.Using ‘ReflectionMethod’ class to get a method information:

Note: I can’t find anything to know whether a method was inherited from its parent class or defined in the reflected class.

 $prnt = $cls->getParentClass();

if ($cls->hasMethod($meth->name)) {
echo "Method {$meth->name} in test1\n";
}
if ($prnt->hasMethod($meth->name)) {
echo "Method {$meth->name} in test\n";
}

$meth = new ReflectionMethod($test1, "def");
$cls = $meth->getDeclaringClass();
$prnt = $cls->getParentClass();

if ($cls->hasMethod($meth->name)) {
echo "Method {$meth->name} in test1\n";
}
if ($prnt->hasMethod($meth->name)) {
echo "Method {$meth->name} in test\n";
}

Output:

Method abc in test1
Method abc in test
Method def in test1

3. Using reflection method using invoke:

<?php
class HelloWorld {
    public function getName($name) {
        return 'Hello ' . $name;
    }
}
$reflectionMethod = new ReflectionMethod('HelloWorld', 'getName');
echo $reflectionMethod->invoke(new HelloWorld(), 'Gopi');
?>

Output:

Hello Gopi

4. Using reflection object:

<?php
class A
{
    public function foo()
    {
        return "class A";
    }
}
class B extends A
{
    public function foo()
    {
        return "class B";
    }
}
$b = new B();
$reflection = new ReflectionObject($b);
$parentReflection = $reflection->getParentClass();
$parentFooReflection = $parentReflection->getMethod('foo');
$data = $parentFooReflection->invoke($b);
echo $data;
?>

Output:

Class A

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

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

Send your message