By Harry Fuecks

Practical Reflection in Java

By Harry Fuecks

Via Java.netPractical Reflection in Java (PDF). This is a chapter from O’Reilly’s Hardcore Java. Know it’s not PHP but a worthwhile read anyway. Even if you don’t use Java it’s worth understanding what it’s about, particularily as interop between PHP and Java is starting to look like something PHP developers should be able to cope with, given JSR 223.

Personally think exploring the reflection capabilities of a language is a very good way for existing programmers, with experience of other languages, to pick up new ones. Mark Pilgrim does a great job in Dive into Python for example. Found it a very useful short cut to grasping what Python is about.

Not actually aware of anything discussing PHP’s reflection APIs online right now, other than the manual. PHP4 provides the basics like is_a() and get_class() while PHP5 has a new reflection API (docs currently here). With PHP I guess it begins with print_r() and var_dump() – two very handy ways for programmers to work out what PHP data structures are about.

Terminology Note: in my mental dictionary the terms “reflection” and “introspection” mean the same thing, when it comes to a programming language’s ability to examine itself. Perhaps that needs correction.

  • nucleuz

    Just a note on the reflection documentation: the class names has changed – no more underscores in the class names, the basic example should be:
    Reflection::export(new ReflectionClass(‘Exception’));
    and not
    Reflection::export(new Reflection_Class(‘Exception’));
    that goes for all the classes in the documentation.

  • sike

    the reflection api provides some good
    insight into its class layout and organization.
    while toying around with it, i found it a nice
    exercise writing a quick and dirty xmi writer.
    since then i use it from time to time to get a impression
    on things im working or to just peek into a libary.

    reflection is a neat new feature and i expect it to get a lot of attention in the future (testing frameworks, documentation…)

  • sike

    upps.. some words missing (:

    the reflection api provides some good insight into class layout and organization of librarys or frameworks (provided they use OO).

  • Rob Clevenger

    This book from O’Reilly simply rawks. Read this excellent review here:,id,283,nodeid,148.html

Get the latest in Front-end, once a week, for free.