Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1
CHAPTER 7 ■ REFLECTION API^89

public function printDocTokens() {
foreach($this->_tokens as $token) {
echo $token[0]. '=';
echo docblock_token_name($token[0]). '=';
print_r($token[1]);
echo "\n";
}
}

public function getParsedTags() {
return $this->_tags;
}

public function getParsedComments() {
return $this->_comments;
}

}

The DocumentingReflectionMethod class extends from ReflectionMethod, inheriting all the
abilities of ReflectionMethod. Then it initializes the base class by calling parent::__construct.
This parent construction must be done as the first line in the overridden constructor; other-
wise, you may get errors or, even worse, code that crashes without any error explanation.
After the base class is initialized, the documentation extensions go into effect. The class
calls the static method you previously defined to parse the doccomment, passing in its own
doccomment, and storing its results in several protected member variables. Finally, several
accessor methods are added to allow you to see that it’s all working.
With all the code in Listings 7-12 and 7-13 in DocumentingReflection.php, you can test the
results so far. Create another file named test.php and add the code shown in Listing 7-14.

Listing 7-14. Testing the DocmentingReflection Classes (test.php)

require_once('DocumentingReflection.php');

class demo {

/**
* This method is for demonstration purposes.
*
* It takes a single parameter and returns it.
*
* @param mixed $param1 A variable to return.
* @returns mixed The input variable is returned.
*/
public function demoMethod($param1) {
return $param1;
}

McArthur_819-9C07.fm Page 89 Friday, February 22, 2008 8:59 AM

Free download pdf