Your SlideShare is downloading. ×

Reflection-In-PHP

394

Published on

PHP 5 comes with a complete reflection API that adds the ability to reverse-engineer classes, interfaces, functions, methods and extensions. Additionally, the reflection API offers ways to retrieve …

PHP 5 comes with a complete reflection API that adds the ability to reverse-engineer classes, interfaces, functions, methods and extensions. Additionally, the reflection API offers ways to retrieve classes, methods, functions information which can be used in several ways like generation HTML forms and creating Database tables by using information provided by PHP Reflection API and much more.

Published in: Software, Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
394
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
6
Comments
0
Likes
3
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 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
  • 19. www.mindfiresolutions.com https://www.facebook.com/MindfireSolutions http://www.linkedin.com/company/mindfire-solutions http://twitter.com/mindfires
  • 20. References: PHP Manual - http://php.net/manual/en/intro.reflection.php TutsPlus - http://code.tutsplus.com/tutorials/reflection-in-php--net-31408

×