Reflection in PHP 
Presenter: Deepak Rai, Mindfire Solutions 
Date: 30/06/2014
About me: 
Skills: 
PHP, MySQL, JavaScript, jQuery, HTML, CSS CodeIgniter, 
StoredProcedures, SVN, GIT. 
Certifications: 
– Oracle Certified Professional (OCP) MySQL 
– Oracle Certified Associate (OCA) MySQL 
– Microsoft Certified HTML5, CSS3 and Javascript programmer 
Presenter: Deepak Rai, Mindfire Solutions 
Contact me: 
Skype: mfsi_deepakr 
E-mail: deepakr@mindfiresolutions.com 
Facebook: www.facebook.com/deepakrai89 
LinkedIn: https://www.linkedin.com/in/deepakrai89 
Google Plus: https://plus.google.com/114288591774549019180/ 
Twitter: www.twitter.com/__deepakrai
Agenda 
– Introduction to PHP Reflection API 
– How to use it from command line 
– Different classes available in API 
– How to get more information about a Class or a Method using this API 
– Examples of creating HTML Forms and Database tables 
– Different PHP well known frameworks using this API 
– Benefits of using Reflection API 
Presenter: Deepak Rai, Mindfire Solutions
What is Reflection in Programming 
Languages ? 
Reflection is generally defined as a program's ability to inspect 
itself and modify its logic at execution time. In less technical 
terms, reflection is asking an object to tell you about its 
properties and methods, and altering those members. 
Presenter: Deepak Rai, Mindfire Solutions
Using Reflection API from command line 
$ php --rf strlen 
Function [ <internal:Core> function strlen ] { 
- Parameters [1] { 
Parameter #0 [ <required> $str ] 
} 
} 
$ php --rf array_merge 
Function [ <internal:standard> function array_merge ] { 
- Parameters [3] { 
Parameter #0 [ <required> $arr1 ] 
Parameter #1 [ <required> $arr2 ] 
Parameter #2 [ <optional> $... ] 
} 
} 
Presenter: Deepak Rai, Mindfire Solutions
Classes available in PHP Reflection API 
Class Name Description 
ReflectionClass Gives information about a class 
ReflectionFunction Reports information about a function 
ReflectionMethod Reports information about a method 
ReflectionParameter Retrives information about method's or 
function's parameter 
ReflectionProperty Gives information about a properties 
ReflectionObject Gives information about an object 
Few more classes are available in Reflection API apart from above mentioned. 
Presenter: Deepak Rai, Mindfire Solutions
Methods available in PHP “ReflectionClass” 
Class 
Method Name Description 
getProperties Gets all properites of the class 
getMethods Gets all methods of the class 
getDocComment Get Class Document comment 
getFileName Gets the file name of the file in which the class 
has been defined 
getParentClass Gets parent class 
hasMethod Checks if method is defined 
ReflectionClass has a long list of methods. 
Presenter: Deepak Rai, Mindfire Solutions
Using 'ReflectionClass' class to get class 
information 
<?php 
class myClass{ 
public $var; 
. . . 
public function myFun(){ 
. . . 
} 
} 
$myClassObj = new myClass(); 
$myClassRef = new ReflectionClass($myClassObj); 
print_r($myClassRef); 
print_r($myClassRef->getProperties()); 
print_r($myClassRef->getMethods()); 
ReflectionClass Object 
( 
[name] => myClass 
) 
Array 
( 
[0] => ReflectionProperty Object 
( 
[name] => var 
[class] => myClass 
) 
) 
Array 
( 
[0] => ReflectionMethod Object 
( 
[name] => myFun 
[class] => myClass 
) 
) 
Presenter: Deepak Rai, Mindfire Solutions
Using 'ReflectionMethod' class to get a 
method information 
class sumClass{ 
/** 
* @access public 
* @var $a 
* @var $b 
*/ 
public function setNum($a, $b = 5){ 
. . . 
. . . 
} 
} 
$sumObj = new sumClass(); 
$setNumMethodReflection = new 
ReflectionMethod($sumObj, 'setNum'); 
print_r($setNumMethodReflection); 
echo $setNumMethodReflection; 
/** 
* @access public 
* @var $a 
* @var $b 
*/ 
Method [ <user> public method setNum ] { 
@@ 
/home/deepak/Desktop/ReflectionPHP/ReflectionMeth 
odExample.php 25 - 31 
- Parameters [2] { 
Parameter #0 [ <required> $a ] 
Parameter #1 [ <optional> $b = 5 ] 
} 
} 
Presenter: Deepak Rai, Mindfire Solutions
Applications 
of PHP Reflection 
API 
Presenter: Deepak Rai, Mindfire Solutions
Generating HTML form from a PHP class 
In Models, generally a Model class represents a DB table for which data is taken by HTML 
form 
class Person { 
private $first_name; 
private $last_name; 
private $age; 
private $email; 
/** 
* Set first name 
*/ 
public function set_first_name($first_name){ 
$this->first_name = $first_name; 
} 
/** 
* Set last name 
*/ 
public function set_last_name($last_name){ 
$this->last_name = $last_name; 
} 
/** 
* Set age 
*/ 
public function set_age($age){ 
$this->age = $age; 
} 
/** 
* Set email 
*/ 
public function set_email($email){ 
$this->email = $email; 
} 
} 
Presenter: Deepak Rai, Mindfire Solutions
Generating HTML form from a PHP class 
In Models, generally a Model class represents a DB table for which data is taken by 
HTML form (continued..) 
// include the class 
require_once 'Person.php'; 
$person_reflection = new ReflectionClass('Person'); 
// get all methods of Class 'Person' 
$person_methods = $person_reflection->getMethods(); 
// output html form 
create_html_form($person_methods); 
function create_html_form($person_methods){ 
/* loop through each method and create html element for each setter method */ 
foreach($person_methods as $method){ 
$method_name = $method->getName(); 
/* if method name prefix is 'set_' the create form element */ 
if(strpos($method_name, 'set_') !== FALSE){ 
echo '<label for="'.substr($method_name,4).'">' 
.str_replace('_',' ',substr($method_name,4)) 
.'</label>';echo "<br/>"; 
echo '<input type="text" name="'.substr($method_name,4).'" />'; 
echo "<br/>"; 
} 
} 
} 
Presenter: Deepak Rai, Mindfire Solutions
Creating DB Table from PHP Class 
<?php 
/** 
* @table="task" 
*/ 
class Task 
{ 
/** 
* @Column(type="int", strategy="auto") 
*/ 
protected $id; 
/** 
* @Column(type="text") 
*/ 
protected $taskDescription; 
/** 
* @Column(type="date") 
*/ 
protected $dueDate; 
} 
We can create Database tables using 
PHP reflection API. 
We can read Class and Properties Doc 
comments to filter it out to extract the 
useful information like table name 
So, here table name is task. Columns 
are id, taskDescription and dueDate 
Presenter: Deepak Rai, Mindfire Solutions
PHP Class Documentation Tool 
We can use PHP Reflection API to generate 
documentation of any PHP class. 
DEMO 
Presenter: Deepak Rai, Mindfire Solutions
PHP Frameworks using PHP API 
- PHPUnit Framework. 
- Code Analysis frameworks (RIPS, a source code analyser). 
- CakePHP (AclExtra library uses Reflection API to find all controllers and 
methods). 
- Laravel uses Reflection API for dependency injection. 
- Doctrine ORM uses Reflection API for working with the entities. 
Presenter: Deepak Rai, Mindfire Solutions
Benefits of using Reflection 
- Duck typing is not possible with Reflection API. 
- HTML Form Generation. 
- Creating DB Tables. 
- Creating Documentation of poorly documented 3rd party classes. 
- Accessing private properties and methods. 
Presenter: Deepak Rai, Mindfire Solutions
? 
Presenter: Deepak Rai, Mindfire Solutions
Thank you 
Presenter: Deepak Rai, Mindfire Solutions
www.mindfiresolutions.com 
https://www.facebook.com/MindfireSolutions 
http://www.linkedin.com/company/mindfire-solutions 
http://twitter.com/mindfires
References: 
PHP Manual - http://php.net/manual/en/intro.reflection.php 
TutsPlus - http://code.tutsplus.com/tutorials/reflection-in-php--net-31408

Reflection-In-PHP

  • 1.
    Reflection in PHP Presenter: Deepak Rai, Mindfire Solutions Date: 30/06/2014
  • 2.
    About me: Skills: PHP, MySQL, JavaScript, jQuery, HTML, CSS CodeIgniter, StoredProcedures, SVN, GIT. Certifications: – Oracle Certified Professional (OCP) MySQL – Oracle Certified Associate (OCA) MySQL – Microsoft Certified HTML5, CSS3 and Javascript programmer Presenter: Deepak Rai, Mindfire Solutions Contact me: Skype: mfsi_deepakr E-mail: deepakr@mindfiresolutions.com Facebook: www.facebook.com/deepakrai89 LinkedIn: https://www.linkedin.com/in/deepakrai89 Google Plus: https://plus.google.com/114288591774549019180/ Twitter: www.twitter.com/__deepakrai
  • 3.
    Agenda – Introductionto PHP Reflection API – How to use it from command line – Different classes available in API – How to get more information about a Class or a Method using this API – Examples of creating HTML Forms and Database tables – Different PHP well known frameworks using this API – Benefits of using Reflection API Presenter: Deepak Rai, Mindfire Solutions
  • 4.
    What is Reflectionin Programming Languages ? Reflection is generally defined as a program's ability to inspect itself and modify its logic at execution time. In less technical terms, reflection is asking an object to tell you about its properties and methods, and altering those members. Presenter: Deepak Rai, Mindfire Solutions
  • 5.
    Using Reflection APIfrom command line $ php --rf strlen Function [ <internal:Core> function strlen ] { - Parameters [1] { Parameter #0 [ <required> $str ] } } $ php --rf array_merge Function [ <internal:standard> function array_merge ] { - Parameters [3] { Parameter #0 [ <required> $arr1 ] Parameter #1 [ <required> $arr2 ] Parameter #2 [ <optional> $... ] } } Presenter: Deepak Rai, Mindfire Solutions
  • 6.
    Classes available inPHP Reflection API Class Name Description ReflectionClass Gives information about a class ReflectionFunction Reports information about a function ReflectionMethod Reports information about a method ReflectionParameter Retrives information about method's or function's parameter ReflectionProperty Gives information about a properties ReflectionObject Gives information about an object Few more classes are available in Reflection API apart from above mentioned. Presenter: Deepak Rai, Mindfire Solutions
  • 7.
    Methods available inPHP “ReflectionClass” Class Method Name Description getProperties Gets all properites of the class getMethods Gets all methods of the class getDocComment Get Class Document comment getFileName Gets the file name of the file in which the class has been defined getParentClass Gets parent class hasMethod Checks if method is defined ReflectionClass has a long list of methods. Presenter: Deepak Rai, Mindfire Solutions
  • 8.
    Using 'ReflectionClass' classto get class information <?php class myClass{ public $var; . . . public function myFun(){ . . . } } $myClassObj = new myClass(); $myClassRef = new ReflectionClass($myClassObj); print_r($myClassRef); print_r($myClassRef->getProperties()); print_r($myClassRef->getMethods()); ReflectionClass Object ( [name] => myClass ) Array ( [0] => ReflectionProperty Object ( [name] => var [class] => myClass ) ) Array ( [0] => ReflectionMethod Object ( [name] => myFun [class] => myClass ) ) Presenter: Deepak Rai, Mindfire Solutions
  • 9.
    Using 'ReflectionMethod' classto get a method information class sumClass{ /** * @access public * @var $a * @var $b */ public function setNum($a, $b = 5){ . . . . . . } } $sumObj = new sumClass(); $setNumMethodReflection = new ReflectionMethod($sumObj, 'setNum'); print_r($setNumMethodReflection); echo $setNumMethodReflection; /** * @access public * @var $a * @var $b */ Method [ <user> public method setNum ] { @@ /home/deepak/Desktop/ReflectionPHP/ReflectionMeth odExample.php 25 - 31 - Parameters [2] { Parameter #0 [ <required> $a ] Parameter #1 [ <optional> $b = 5 ] } } Presenter: Deepak Rai, Mindfire Solutions
  • 10.
    Applications of PHPReflection API Presenter: Deepak Rai, Mindfire Solutions
  • 11.
    Generating HTML formfrom a PHP class In Models, generally a Model class represents a DB table for which data is taken by HTML form class Person { private $first_name; private $last_name; private $age; private $email; /** * Set first name */ public function set_first_name($first_name){ $this->first_name = $first_name; } /** * Set last name */ public function set_last_name($last_name){ $this->last_name = $last_name; } /** * Set age */ public function set_age($age){ $this->age = $age; } /** * Set email */ public function set_email($email){ $this->email = $email; } } Presenter: Deepak Rai, Mindfire Solutions
  • 12.
    Generating HTML formfrom a PHP class In Models, generally a Model class represents a DB table for which data is taken by HTML form (continued..) // include the class require_once 'Person.php'; $person_reflection = new ReflectionClass('Person'); // get all methods of Class 'Person' $person_methods = $person_reflection->getMethods(); // output html form create_html_form($person_methods); function create_html_form($person_methods){ /* loop through each method and create html element for each setter method */ foreach($person_methods as $method){ $method_name = $method->getName(); /* if method name prefix is 'set_' the create form element */ if(strpos($method_name, 'set_') !== FALSE){ echo '<label for="'.substr($method_name,4).'">' .str_replace('_',' ',substr($method_name,4)) .'</label>';echo "<br/>"; echo '<input type="text" name="'.substr($method_name,4).'" />'; echo "<br/>"; } } } Presenter: Deepak Rai, Mindfire Solutions
  • 13.
    Creating DB Tablefrom PHP Class <?php /** * @table="task" */ class Task { /** * @Column(type="int", strategy="auto") */ protected $id; /** * @Column(type="text") */ protected $taskDescription; /** * @Column(type="date") */ protected $dueDate; } We can create Database tables using PHP reflection API. We can read Class and Properties Doc comments to filter it out to extract the useful information like table name So, here table name is task. Columns are id, taskDescription and dueDate Presenter: Deepak Rai, Mindfire Solutions
  • 14.
    PHP Class DocumentationTool We can use PHP Reflection API to generate documentation of any PHP class. DEMO Presenter: Deepak Rai, Mindfire Solutions
  • 15.
    PHP Frameworks usingPHP API - PHPUnit Framework. - Code Analysis frameworks (RIPS, a source code analyser). - CakePHP (AclExtra library uses Reflection API to find all controllers and methods). - Laravel uses Reflection API for dependency injection. - Doctrine ORM uses Reflection API for working with the entities. Presenter: Deepak Rai, Mindfire Solutions
  • 16.
    Benefits of usingReflection - Duck typing is not possible with Reflection API. - HTML Form Generation. - Creating DB Tables. - Creating Documentation of poorly documented 3rd party classes. - Accessing private properties and methods. Presenter: Deepak Rai, Mindfire Solutions
  • 17.
    ? Presenter: DeepakRai, Mindfire Solutions
  • 18.
    Thank you Presenter:Deepak Rai, Mindfire Solutions
  • 19.
  • 20.
    References: PHP Manual- http://php.net/manual/en/intro.reflection.php TutsPlus - http://code.tutsplus.com/tutorials/reflection-in-php--net-31408