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 />"; ?>
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:
PHPDoc: Useful for automatically generating code documentation based on comments embedded in the source code (seehttp://www.pear.php.net/package/PHPDoc)
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.