Made of Everything You're Not

Personal blog of PHP programmer Eric Lamb.
  • Blog
  • Portfolio
« The Lesson of Adobe Reader
The Commodity HTPC Dream is Finally Realized »

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.

Related Posts

ACM Interactions
Stand Alone ExpressionEngine Authentication
MSRC
Importing Legacy Users Into ExpressionEngine
Nesting Platform

Tags: php reflection api

This entry was written by Eric Lamb and posted on March 01st, 2011 at 12:00 am and is filed under Brain Dump, Code, Programming. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response below.

13 Comments

  1. Eric Lamb’s Blog: Practical PHP Reflection | says:
    March 05, 2011 at 02:14 am

    [...] 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 [...]

    Reply | Quote
  2. Cheatsheet: 2011 03.01 ~ 03.06 - gOODiDEA.NET says:
    March 05, 2011 at 06:30 pm

    [...] Practical PHP Reflection [...]

    Reply | Quote
  3. Peteru says:
    March 06, 2011 at 01:32 am

    This article is not readable on iPad

    Reply | Quote
  4. A semana no mundo PHP (11/03/2011) | raphael.dealm says:
    March 11, 2011 at 03:46 am

    [...] Practical PHP Reflection [...]

    Reply | Quote
  5. Toni says:
    March 15, 2011 at 02:34 pm

    who matters?

    Reply | Quote
  6. Tom says:
    August 06, 2011 at 11:55 pm

    Hahaha! What a fucking shame!

    Reply | Quote
  7. Haha says:
    August 10, 2011 at 02:01 am

    Care face ^_^

    Nice article

    Reply | Quote
  8. Backslider says:
    April 23, 2012 at 10:10 am

    “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.

    Reply | Quote
  9. STEVER says:
    July 29, 2012 at 05:59 am

    thx for examples of reflection

    Reply | Quote
  10. artodeto's blog about coding, politics and the wor says:
    August 30, 2012 at 12:22 pm

    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…

    Reply | Quote
  11. Mariko says:
    January 08, 2013 at 07:05 am

    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.

    Reply | Quote
  12. hineheera says:
    March 21, 2013 at 10:13 pm

    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

    Reply | Quote
  13. antabatiack says:
    March 22, 2013 at 03:11 am

    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 アウトレット

    Reply | Quote

Leave a Reply

Click here to cancel reply.

  • Subscribe: Entries | Comments
  • About Me

    Email Email
    Twitter Twitter
    310.739.3322
  • Categories

    • Brain Dump
    • Business
    • Code
    • IT
    • Programming
    • Rant
    • Servers
  • Archives

    • February 2012
    • October 2011
    • August 2011
    • July 2011
    • June 2011
    • May 2011
    • April 2011
    • March 2011
    • February 2011
    • January 2011
    • December 2010
    • November 2010
    • October 2010
    • September 2010
    • August 2010
    • July 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • September 2009
    • August 2009
    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
  • Advertisement

Copyright © 2008 - 2013 Eric Lamb - All rights reserved