Practical PHP Reflection
The Reflection API in PHP is one of those language features that can kind of creep up on you; it's just not needed for a vast majority of projects. In fact, I'd wager there are a bunch of professional developers who have never had to make use of the Reflection API. Hell, I spent 8 years writing code professionally before I ever had to use it (though, because of some unrelated .NET research I was peripherally aware of it). It's just not something that really comes up all that often in the day to day coding tasks.
Then, one day, out of nowhere, it comes up. The perfect problem where the Reflection API appears to be the perfect solution. For me this came up a couple months ago while I was working on a European zip code radius project that had to be built using one of those obfuscated and ill documented 3rd party commercial programs (nothing worse than when the platform is forced on you). So, I had to use this program that was intentionally encoded to prevent me from doing what I wanted to do. I couldn't even look at the code; it was completely obfuscated.
Before getting into things it should be noted that the Reflection API is capable above and beyond what I'm outlining; usually for the kind of high level stuff like core Zend Framework components and anything you'd want way abstracted.
So, yeah, weird problem.
This is where reflection comes in for me. It was made for problems like the above; according to the manual:
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 doc comments for functions, classes and methods.
So, the theory was that I could use the Reflection API to look into the obfuscated code and get some insight into what was going on and what I had to work with. I figured that, at the least, if I could see what methods, properties and comments the code had; maybe I'd get some clue about what the code was about. Using, for example, the internal PHP class "SimpleXMLElement" it's real simple to get a nice map of the class by calling the class ReflectionClass and passing the name of the class you want to get details on as a parameter like the below:
1 2 3 4 | <?php $reflector = new ReflectionClass("SimpleXMLElement"); echo $reflector; ?> |
The above outputs an eye gouging but verbose class map:
Class { - Constants { } - Static properties { } - Static methods { } - Properties { } - Methods { Method { } Method { } Method { } Method { } Method { } Method { } Method { } Method { } Method { } Method { } Method { } Method { } Method { } Method { } } }
As you can see, the above lists every property, method and constant about a class along with any parent classes and interfaces. If there were any variables the methods accepted it would also list those as well but SimpleXMLElement doesn't have any.
It's also possible to get the individual elements on a one by one basis while using the ReflectionClass object; for example the below methods returns the class name, any comments (if they're in docblock format), the constants, parent class, constructor and default properties for the class DateTime.
<?php $reflector = new ReflectionClass("DateTime"); echo "Class name: ".$reflector->getName()."\n"; echo "Doc Comment:".var_dump($reflector->getDocComment())."\n"; echo "Contants: ".var_dump($reflector->getConstants())."\n"; echo "Parent Class: ".var_dump($reflector->getParentClass())."\n"; echo "Constructor: ".var_dump($reflector->getConstructor())."\n"; echo "Default Properties: ".var_dump($reflector->getDefaultProperties())."\n"; ?>
Outputs
Class name: DateTime Doc Comment: bool(false) Contants: array(11) { => string(13) "Y-m-d\TH:i:sP" => string(16) "l, d-M-y H:i:s T" => string(13) "Y-m-d\TH:i:sO" => string(16) "D, d M y H:i:s O" => string(16) "l, d-M-y H:i:s T" => string(16) "D, d M y H:i:s O" => string(16) "D, d M Y H:i:s O" => string(16) "D, d M Y H:i:s O" => string(13) "Y-m-d\TH:i:sP" => string(16) "D, d M Y H:i:s O" => string(13) "Y-m-d\TH:i:sP" } Parent Class: bool(false) Constructor: object(ReflectionMethod)#2 (2) { => string(11) "__construct" => string(8) "DateTime" } Default Properties: array(0) { }
Check out the documentation; there are a bunch of other details you can get from other methods.
This is really just the tip of the iceberg though. There are other classes that allow even deeper insight into functions, methods, extensions and much more. Definitely worth checking out.
Email
Twitter
[...] an interesting situation recently and found that PHP’s Reflection API was the best thing to come to his rescue. The perfect problem where the Reflection API appears to be the perfect solution. For me this came [...]
[...] Practical PHP Reflection [...]
This article is not readable on iPad
[...] Practical PHP Reflection [...]
who matters?
Hahaha! What a fucking shame!
Care face ^_^
Nice article
“Peteru says:
March 6, 2011 at 1:32 am
This article is not readable on iPad”
Ha ha ha ha… thats funny. Your “walled garden” is protecting you…. there must be something on the page that Apple sees as a threat.
thx for examples of reflection
notes - froscon 2012 - alternative php runtimes - sebastian bergmann…
Following are my notes to sebastians bergmann presentation. The presentation was quite good (you can add “as usual” if you have seen sebastian bergmann more than once ). #120825 sebastian bergmann _general - he found at least 10, some ready for produ…
I feel that social minxig can be for the betterment of neighborhoods. The minxig of different social classes can enhance the economical status of neighborhoods, however I feel by doing that it will raise living costs for the poor thus resulting in their displacement. Poverty is deconcentrated for a period of time up until the poor realize that their living cost are too high thanks to their new rich neighbors and then they eventually move out into another poverty stricken neighborhood. I feel these strategies only answer the problem in the present time but leave the situation unanswered in the long run. When I think of what other ways poverty can be overcome I am left frazzled. The only answer I can think of is a revitalization project but with the same living costs after the project as it was before. As of right now, after doing some research I feel social minxig and poverty deconcentration are only temporary answers to this worldwide issue.
MuE kfBO z lzIO TUMI 26114 http://toumijaeonnrainn.com/ Fc NyZ UoR wxMR TUMI 26141 DH alpha JhU bmCZ y sbPD トゥミ http://toumijapsaisin.com/ ToUdn hvQj wqHd zhLp TUMI 財布 ArI koSJ t thTI プラダ 財布 新作 http://puradajaeninnki.com/ Ud ViI ExY eqEA プラダ キーケース ViH frWH l ezQM レイバン 眼鏡 http://sugurereiban.com/ Ar PtY IeQ pxJO レイバン アウトレット NfW zcEF y rlSL o http://louisrtvuittonrtluggage.webs.com/ KuUao byYh psOb fpOu louis vuitton luggage SqK qeKM y psSV chanel バッグ http://syanerujaesinsaku.com/ RjQkd meRt pwZs elIi シャネル 財布 IoE ieCA j spAC シャネル http://syanerujaesenmon.com/ HcKif aqFy sqAl nqKx シャネル 財布 2013 FmQ kwHD m yoVH プラダ 財布 新作 http://puradajaeonnrainn.com/ MsWul iyNu itXm efCg プラダ トート デニム RzY xaSX p lqPB ビジネスバッグ TUMI http://toumijaebaggu.com/ Ms JoV YbK ksLS TUMI 26141 DH alpha
Pp GjV WbF pjEV p http://yichibanoakley.com/ KezTt Tmn Ucs Lkf YvtTg Oakley メガネ Hj KrI RpO qtPW k http://nihonsgucchi.com/ IynPp Ofb Uop Zsy YirWm GUCCI チョコレート Ys YuK RgE wtMI u http://guccisnihon.com/ BglSp Dua Xia Szg ZqzDr グッチ アウトレット Cj MjM BcZ amQS f http://oakleysaiyasune.com/ HatYy Nfi Ftl Dhf VqcKq Oakley メガネ Yr VnH FkB pgTE h http://oakleydendou.com/ JppLu Xts Gtj Lxy WcfCj オークリー サングラス We SeP LsN esCR m http://kireiviton.com/ KflZr Qhl Ght Zks LewOj グッチ 財布 メンズ Bz YaE FqF ciTI s http://oakleysalenihon.com/ DfbPf Iel Tqj Oyc OcyHz オークリー サングラス Xp DfD WoE ycOS p http://oakleysenmon.com/ AfyZh Kpj Opm Uul ZqoSb Oakley アウトレット