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

More Related Content

PDF
Intermediate OOP in PHP
PPT
Php Oop
PDF
OOP in PHP
PPTX
Introduction to PHP OOP
ZIP
Object Oriented PHP5
PPTX
Intro to OOP PHP and Github
PPT
Oops in PHP
PPT
Class 7 - PHP Object Oriented Programming
Intermediate OOP in PHP
Php Oop
OOP in PHP
Introduction to PHP OOP
Object Oriented PHP5
Intro to OOP PHP and Github
Oops in PHP
Class 7 - PHP Object Oriented Programming

What's hot (20)

PPT
Intro to OOP and new features in PHP 5.3
PDF
Demystifying Object-Oriented Programming - PHP[tek] 2017
PDF
A Gentle Introduction To Object Oriented Php
PPTX
Object oreinted php | OOPs
PPTX
Object oriented programming in php 5
PPT
Introduction to OOP with PHP
PPT
Class and Objects in PHP
PDF
Object Oriented Programming with PHP 5 - More OOP
PPTX
Oop in-php
PDF
Object Oriented Programming in PHP
PPT
Oops in PHP By Nyros Developer
PPT
Oops concepts in php
PDF
Object-oriented Programming-with C#
PPTX
Php oop presentation
PDF
Take the Plunge with OOP from #pnwphp
PPT
PHP- Introduction to Object Oriented PHP
PPTX
Object-Oriented Programming with C#
PPTX
Ch8(oop)
PPTX
Oops in php
Intro to OOP and new features in PHP 5.3
Demystifying Object-Oriented Programming - PHP[tek] 2017
A Gentle Introduction To Object Oriented Php
Object oreinted php | OOPs
Object oriented programming in php 5
Introduction to OOP with PHP
Class and Objects in PHP
Object Oriented Programming with PHP 5 - More OOP
Oop in-php
Object Oriented Programming in PHP
Oops in PHP By Nyros Developer
Oops concepts in php
Object-oriented Programming-with C#
Php oop presentation
Take the Plunge with OOP from #pnwphp
PHP- Introduction to Object Oriented PHP
Object-Oriented Programming with C#
Ch8(oop)
Oops in php
Ad

Similar to Reflection-In-PHP (20)

PDF
Demystifying Object-Oriented Programming - ZendCon 2016
PPTX
c91632a4-2e92-4edf-b750-358da15ed1b1.pptx
PPT
Basic Oops concept of PHP
PPT
Introduction Php
PPTX
OOP Is More Then Cars and Dogs - Midwest PHP 2017
PDF
Demystifying Object-Oriented Programming - PHP UK Conference 2017
PDF
Design attern in php
PDF
Demystifying oop
PPTX
Oop's in php
PDF
Phpspec tips&amp;tricks
PDF
Demystifying Object-Oriented Programming - Lone Star PHP
PDF
Demystifying Object-Oriented Programming #phpbnl18
PDF
OOP in PHP
PPTX
Coming to Terms with OOP In Drupal - php[world] 2016
PPTX
Lecture-10_PHP-OOP.pptx
PPTX
PHP OOP Lecture - 02.pptx
PPTX
Lecture9_OOPHP_SPring2023.pptx
PDF
Advanced Php - Macq Electronique 2010
PPTX
OOPS IN PHP.pptx
PPTX
Php oop (1)
Demystifying Object-Oriented Programming - ZendCon 2016
c91632a4-2e92-4edf-b750-358da15ed1b1.pptx
Basic Oops concept of PHP
Introduction Php
OOP Is More Then Cars and Dogs - Midwest PHP 2017
Demystifying Object-Oriented Programming - PHP UK Conference 2017
Design attern in php
Demystifying oop
Oop's in php
Phpspec tips&amp;tricks
Demystifying Object-Oriented Programming - Lone Star PHP
Demystifying Object-Oriented Programming #phpbnl18
OOP in PHP
Coming to Terms with OOP In Drupal - php[world] 2016
Lecture-10_PHP-OOP.pptx
PHP OOP Lecture - 02.pptx
Lecture9_OOPHP_SPring2023.pptx
Advanced Php - Macq Electronique 2010
OOPS IN PHP.pptx
Php oop (1)
Ad

More from Mindfire Solutions (20)

PDF
Physician Search and Review
PDF
diet management app
PDF
Business Technology Solution
PDF
Remote Health Monitoring
PDF
Influencer Marketing Solution
PPT
High Availability of Azure Applications
PPTX
IOT Hands On
PPTX
Glimpse of Loops Vs Set
ODP
Oracle Sql Developer-Getting Started
PPT
Adaptive Layout In iOS 8
PPT
Introduction to Auto-layout : iOS/Mac
PPT
LINQPad - utility Tool
PPT
Get started with watch kit development
PPTX
Swift vs Objective-C
ODP
Material Design in Android
ODP
Introduction to OData
PPT
Ext js Part 2- MVC
PPT
ExtJs Basic Part-1
PPT
Spring Security Introduction
Physician Search and Review
diet management app
Business Technology Solution
Remote Health Monitoring
Influencer Marketing Solution
High Availability of Azure Applications
IOT Hands On
Glimpse of Loops Vs Set
Oracle Sql Developer-Getting Started
Adaptive Layout In iOS 8
Introduction to Auto-layout : iOS/Mac
LINQPad - utility Tool
Get started with watch kit development
Swift vs Objective-C
Material Design in Android
Introduction to OData
Ext js Part 2- MVC
ExtJs Basic Part-1
Spring Security Introduction

Recently uploaded (20)

PDF
AI/ML Infra Meetup | LLM Agents and Implementation Challenges
PPTX
Download Adobe Photoshop Crack 2025 Free
PDF
Autodesk AutoCAD Crack Free Download 2025
PDF
BoxLang Dynamic AWS Lambda - Japan Edition
PPTX
Matchmaking for JVMs: How to Pick the Perfect GC Partner
PDF
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
PPTX
Introduction to Windows Operating System
PDF
Practical Indispensable Project Management Tips for Delivering Successful Exp...
PPTX
"Secure File Sharing Solutions on AWS".pptx
PDF
EaseUS PDF Editor Pro 6.2.0.2 Crack with License Key 2025
PDF
AI Guide for Business Growth - Arna Softech
PDF
DuckDuckGo Private Browser Premium APK for Android Crack Latest 2025
PPTX
Computer Software - Technology and Livelihood Education
PDF
How Tridens DevSecOps Ensures Compliance, Security, and Agility
PDF
Guide to Food Delivery App Development.pdf
PDF
Salesforce Agentforce AI Implementation.pdf
PDF
Top 10 Software Development Trends to Watch in 2025 🚀.pdf
PPTX
Full-Stack Developer Courses That Actually Land You Jobs
PPTX
Cybersecurity-and-Fraud-Protecting-Your-Digital-Life.pptx
PPTX
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
AI/ML Infra Meetup | LLM Agents and Implementation Challenges
Download Adobe Photoshop Crack 2025 Free
Autodesk AutoCAD Crack Free Download 2025
BoxLang Dynamic AWS Lambda - Japan Edition
Matchmaking for JVMs: How to Pick the Perfect GC Partner
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
Introduction to Windows Operating System
Practical Indispensable Project Management Tips for Delivering Successful Exp...
"Secure File Sharing Solutions on AWS".pptx
EaseUS PDF Editor Pro 6.2.0.2 Crack with License Key 2025
AI Guide for Business Growth - Arna Softech
DuckDuckGo Private Browser Premium APK for Android Crack Latest 2025
Computer Software - Technology and Livelihood Education
How Tridens DevSecOps Ensures Compliance, Security, and Agility
Guide to Food Delivery App Development.pdf
Salesforce Agentforce AI Implementation.pdf
Top 10 Software Development Trends to Watch in 2025 🚀.pdf
Full-Stack Developer Courses That Actually Land You Jobs
Cybersecurity-and-Fraud-Protecting-Your-Digital-Life.pptx
WiFi Honeypot Detecscfddssdffsedfseztor.pptx

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 – 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
  • 4. 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
  • 5. 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
  • 6. 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
  • 7. 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
  • 8. 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
  • 9. 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
  • 10. Applications of PHP Reflection API Presenter: Deepak Rai, Mindfire Solutions
  • 11. 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
  • 12. 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
  • 13. 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
  • 14. PHP Class Documentation Tool We can use PHP Reflection API to generate documentation of any PHP class. DEMO Presenter: Deepak Rai, Mindfire Solutions
  • 15. 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
  • 16. 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
  • 17. ? Presenter: Deepak Rai, Mindfire Solutions
  • 18. Thank you Presenter: Deepak Rai, Mindfire Solutions
  • 20. References: PHP Manual - http://php.net/manual/en/intro.reflection.php TutsPlus - http://code.tutsplus.com/tutorials/reflection-in-php--net-31408