Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP EU 2016)
Reflection-In-PHP
-
Upload
mindfire-solutions -
Category
Software
-
view
387 -
download
0
description
Transcript of Reflection-In-PHP
Reflection in PHP
Presenter: Deepak Rai, Mindfire SolutionsDate: 30/06/2014
Presenter: Deepak Rai, Mindfire Solutions
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
Contact me: Skype: mfsi_deepakr E-mail: [email protected]
Facebook: www.facebook.com/deepakrai89 LinkedIn: https://www.linkedin.com/in/deepakrai89 Google Plus: https://plus.google.com/114288591774549019180/ Twitter: www.twitter.com/__deepakrai
Presenter: Deepak Rai, Mindfire Solutions
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
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
Presenter: Deepak Rai, Mindfire Solutions
Few more classes are available in Reflection API apart from above mentioned.
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
Presenter: Deepak Rai, Mindfire SolutionsReflectionClass has a long list of methods.
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/ReflectionMethodExample.php 25 - 31
- Parameters [2] {
Parameter #0 [ <required> $a ]
Parameter #1 [ <optional> $b = 5 ]
}
}
Presenter: Deepak Rai, Mindfire Solutions
Applicationsof PHP Reflection
API
Presenter: Deepak Rai, Mindfire Solutions
Generating HTML form from a PHP classIn Models, generally a Model class represents a DB table for which data is taken by
HTML formclass 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 classIn 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
Presenter: Deepak Rai, Mindfire Solutions
We can use PHP Reflection API to generate documentation of any PHP class.
DEMO
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
?
Presenter: Deepak Rai, Mindfire Solutions
Thank you
www.mindfiresolutions.com
https://www.facebook.com/MindfireSolutions
http://www.linkedin.com/company/mindfire-solutions
http://twitter.com/mindfires
PHP Manual - http://php.net/manual/en/intro.reflection.php
TutsPlus - http://code.tutsplus.com/tutorials/reflection-in-php--net-31408
References: