Miscellaneous

  Home arrow Miscellaneous arrow Page 3 - Reflection in PHP 5
MISCELLANEOUS

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

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

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Reflection in PHP 5 - Writing the ReflectionProperty Class


    (Page 3 of 3 )

    The ReflectionProperty class is used to learn more about a particular class’s properties. Listing 7-7 presents the ReflectionProperty class contents. An example demonstrating this class’s capabilities follows the listing.

    Listing 7-7. The ReflectionProperty Class

    class ReflectionProperty implements Reflector
    {
       final private __clone()
       public __construct(mixed class, string name)
       public string __toString()

       public static string export()

       public ReflectionClass getDeclaringClass()
       public string getDocComment() # introduced in PHP 5.1.0
       public int getModifiers()
       public string getName()
       public mixed getValue(stdclass object)

       public bool isPublic()
       public bool isPrivate()
       public bool isProtected()
       public bool isStatic()
       public bool isDefault()

       public void setValue(stdclass object, mixed value)

    }

    Let’s use theReflectionProperty class to learn more about thecorporatedrone class’s properties (thecorporatedroneclass is found in Listing 7-1):

    <?php
      
    $method = new ReflectionClass("corporatedrone"); 

       $properties = $method->getProperties();

       foreach ($properties as $property) echo $property->getName()."<br />";
    ?>

    This example returns the following output:

    --------------------------------------------
    employeeid
    tiecolor
    --------------------------------------------

    Other Reflection Applications

    While reflection is useful for purposes such as those described in the preceding sections, you may be surprised to know that it can also be applied to a variety of tasks, including testing code, generating documentation, and performing other duties. For instance, the following two PEAR packages depend upon the reflection API to carry out their respective tasks: 

    1. PHPDoc: Useful for automatically generating code documentation based on comments embedded in the source code (seehttp://www.pear.php.net/package/PHPDoc)
    2. PHPUnit2: A testing framework for performing unit tests (seehttp://www.pear.php.net/package/PHPUnit2)

    Consider examining the contents of these packages to learn about the powerful ways in which they harness reflection to carry out useful tasks.

    Summary

    This and the previous chapter introduced you to the entire gamut of PHP’s OOP features, both old and new. Although the PHP development team was careful to ensure that users aren’t constrained to use these features, the improvements and additions made regarding PHP’s ability to operate in conjunction with this important development paradigm represent a quantum leap forward for the language. If you’re an old hand at object-oriented programming, hopefully these last two chapters have left you smiling ear-to-ear over the long-awaited capabilities introduced within these pages. If you’re new to OOP, the material should help you to better understand many of the key OOP concepts and inspire you to perform additional experimentation and research.

    The next chapter introduces yet another new, and certainly long-awaited, feature of PHP 5: exception handling.  


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.
    blog comments powered by Disqus

    MISCELLANEOUS ARTICLES

    - 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
    - Install Slackware on Your Old PC


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