Joel Simpson presented on PHP reflection and annotation driven development at the Seattle PHP User's Group on September 12, 2013. He discussed PHP's built-in reflection methods, the ReflectionAPI, and how reflection can be used at development time for documentation and autocomplete and at runtime for request routing, ORM/DBAL, design patterns, and aspect oriented programming. He then demonstrated how to use annotations to implement basic request routing in under 100 lines of code.
2. 15+ yrs professional software engineering
and consulting experience (~6 yrs with PHP)
Founder, CEO of Alegion®
Enterprise Crowdsourcing as a Service
Lover of Open Source Software (and PHPStorm)
..and far too many interests than would fit readably on this slide:
Alegion, sailing, go/weiqi/igo/baduk, micro-controllers and electronics, shakuhachi, settlers of catan
style board games, 3d modeling/rendering/printing, science, technology, start-ups, philosophy, process
engineering, soccer, mountain biking, snow boarding, hiking, obstacle course racing (Spartan), Zen.
3. It’s Meta
PHP 4, the early predecessors, built in methods:
method_exists()
class_exists()
is_a()
is_subclass_of()
get_parent_class()
4. class_exists — Checks if the class has been defined
get_called_class — the "Late Static Binding" class name
get_class_methods — Gets the class methods' names
get_class_vars — Get the default properties of the class
get_class — Returns the name of the class of an object
get_declared_classes — Returns an array with the name of the defined classes
get_declared_interfaces — Returns an array of all declared interfaces
get_declared_traits — Returns an array of all declared traits
get_object_vars — Gets the properties of the given object
get_parent_class — Retrieves the parent class name for object or class
interface_exists — Checks if the interface has been defined
is_a — Checks if the object is of this class or has this class as one of its parents
is_subclass_of — Checks if the object has this class as one of its parents
method_exists — Checks if the class method exists
property_exists — Checks if the object or class has a property
trait_exists — Checks if the trait exists
Source: http://www.php.net/manual/en/ref.classobj.php
5. The ReflectionAPI
http://php.net/manual/en/book.reflection.php
BIG, comprehensive, but not complex
7. A function that populates properties automatically for
quickly smoke testing/debugging field mapping in a
class.
Our example, a User:
FirstName
LastName
UserName
EmailAddress
Password
8. Annotation Driven Development (ADD)
Describe/configure right along side the code
Advanced ExampleChallenge
Write a rudimentary “frontController” in 100 lines
of code or less
Implement request routing via annotations
▪ @url /my/route/
9. class Router{private static $a=array();static function
route($b){if(count(self::$a)==0){self::loadRoutableClasses();}foreach(self:
:$a as $c){$d=new ReflectionClass($c);foreach($d->getMethods()as
$e){$f=$e->getDocComment();preg_match_all('/@urls+[
t]*/?(S*)/s',$f,$g,PREG_SET_ORDER);if(!$g){continue;}foreach($g[0]as
$h){if(self::cleanUrl($b)==self::cleanUrl($h)){$i=$d-
>newInstance();$j=$e->getShortName();return $i->$j();}}}}return(new
DefaultController())->notFoundAction();}private static function
cleanUrl($b){return strtolower(trim($b,"/"));}private static function
loadRoutableClasses(){foreach(get_declared_classes()as
$c){if(is_subclass_of($c,'Routable')){self::$a[]=$c;}}}}
Or ~70 lines of readable, commented code. Let’s take a look….
10. Me
Joel.Simpson@gmail.com | skype: joelrsimpson |
Linkedin.com/in/joelsimpson | http://alegion.com
Presentation
Content
Slides:https://Speakerdeck.com/JoelSimpson
Code: http://bit.ly/1b7Oo5o
Additional
Information
http://php.net/manual/en/book.reflection.php
11. C:|$ php –r? <name>
Source: http://www.php.net/manual/en/features.commandline.options.php
--rf <name>
• Show
information
about
function
--rc <name>
• Show
information
about class
--re <name>
• Show
information
about
extension
--rz <name>
• Show
information
about Zend
extension
--ri <name>
• Show
configuration
for extension
(“I” ni)
Notes de l'éditeur
1m - Introduction2m - What is Reflection?2m - What does PHP have for Reflection? (what does the api look like)10m - A cool use case (a very simple, hopefully useful, example - not sure what this should be yet)3m - Overview a short advanced example (e.g. 50 lines of code that implement a front controller to route by annotations on class methods, "specify the url path in the annotation, the method get's called"), posted to github for those who are interested to review later2m - Close