Home arrow Miscellaneous arrow Reflection in PHP 5

Reflection in PHP 5
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 2

    Table of Contents:
  • Reflection in PHP 5
  • Writing the ReflectionMethod Class
  • Writing the ReflectionProperty Class



    Reflection in PHP 5

    (Page 1 of 3 )

    In this conclusion to a three-part series on advanced object-oriented programming features in PHP 5, you'll learn about reflection in detail. This article is excerpted from chapter 7 of the book Beginning PHP and PostgreSQL 8: From Novice to Professional, written by W. Jason Gilmore and Robert H. Treat (Apress; ISBN: 1590595475).


    The classes used as examples in this and the previous chapters were for demonstrational purposes only, and therefore were simplistic enough that most of the features and behaviors could be examined at a single glance. However, real-world applications often require much more complex code. For instance, it isnít uncommon for a single application to consist of dozens of classes, with each class consisting of numerous members and complex methods. While opening the code in an editor does facilitate review, what if you just want to retrieve a list of all available classes, or all class methods or members for a specific class? Or perhaps youíd like to know the scope of a particular method (abstract, private, protected, public, or static). Sifting through the code to make such determinations can quickly grow tedious.

    The idea of inspecting an object to learn more about it is known as introspection, whereas the process of actually doing so is called reflection. As of version 5, PHP offers a reflection API that is capable of querying not only classes and methods, but also functions, interfaces, and extensions. This section introduces reflection as applied to the review of classes and methods.

    Tip The PHP manual offers more about the other features available to PHPís reflection API. Seehttp://www.php.net/oop5.reflectionfor more information.

    As related to class and method introspection, the PHP reflection API consists of four classes:ReflectionClass,ReflectionMethod,ReflectionParameter, andReflectionProperty. Each class is introduced in turn in the following sections.

    Writing the ReflectionClass Class

    The ReflectionClass class is used to learn all about a class. It is capable of determining whether the class is a child class of some particular parent, retrieving a list of class methods and members, verifying whether the class is final, and much more. Listing 7-4 presents the ReflectionClass class contents. Although it isnít practical to introduce each of the more than 30 methods available to this class, the method names are fairly self-explanatory regarding their purpose. An example follows the listing.

    Listing 7-4. The ReflectionClass Class

    class ReflectionClass implements Reflector
       final private __clone()
       public object __construct(string name) 
       public string __toString()

       public static string export()

       public mixed getConstant(string name)
       public array getConstants()
       public ReflectionMethod getConstructor()
       public array getDefaultProperties()
       public string getDocComment()
       public int getEndLine()
       public string getExtensionName()
       public string getFileName()
       public ReflectionClass[] getInterfaces()
       public ReflectionMethod[] getMethods()
       public ReflectionMethod getMethod(string name)
    public int getModifiers()
       public string getName()
       public ReflectionClass getParentClass()
       public ReflectionProperty[] getProperties()
       public ReflectionProperty getProperty(string name)
       public int getStartLine()
       public array getStaticProperties()

       # The following three methods were introduced in PHP 5.1

       public bool hasConstant(string name)
       public bool hasMethod(string name)
       public bool hasProperty(string name)

       public bool implementsInterface(string name)

       public bool isAbstract()
       public bool isFinal()
       public bool isInstance(stdclass object)
       public bool isInstantiable()
       public bool isInterface()
       public bool isInternal()
       public bool isSubclassOf(ReflectionClass class)
       public bool isIterateable()
       public bool isUserDefined()

       public stdclass newInstance(mixed* args)

       public ReflectionExtension getExtension()


    To seeReflectionClass in action, letís use it to examine thecorporatedrone class first created in Listing 7-1:


       $class = new ReflectionClass("corporatedrone");

       # Retrieve and output class methods
       $methods = $class->getMethods();

       echo "Class methods: <br />";

       foreach($methods as $method)
          echo $method->getName()."<br />";

       # Is the class abstract or final?
       $isAbstract = $class->isAbstract() ? "Yes" : "No";
       $isFinal = $class->isFinal() ? "Yes" : "No";

       echo "<br />";
       echo "Is class ".$class->getName()." Abstract: ".$isAbstract."<br />";
       echo "Is class ".$class->getName()." Final: ".$isFinal."<br />";


    Executing this example returns the following output:

    Class methods:

    Is class corporatedrone Abstract: No
    Is class corporatedrone Final: No

    More Miscellaneous Articles
    More By Apress Publishing

    blog comments powered by Disqus


    - Attention: Forum and Site Maintenance
    - Oracle Database XE: Indexes and Sequences
    - Modifying Tables in Oracle Database XE
    - Oracle Database XE: Tables and Constraints
    - More on Oracle Databases and Datatypes
    - Oracle Database XE Datatypes: Datetime and L...
    - Oracle Database XE Datatypes: Character and ...
    - From Databases to Datatypes
    - Firefox 3.6.6 Released with Improved Plug-in...
    - Attention Bloggers: WordPress 3.0 Now Releas...
    - Reflection in PHP 5
    - Inheritance and Other Advanced OOP Features
    - Advanced OOP Features
    - Linux from Scratch V.6.6 Review
    - Linux Gaining in Strength

    Developer Shed Affiliates


    © 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap