Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1

(^94) CHAPTER 7 ■ REFLECTION API
foreach(parent::getParameters() as $parameter) {
$parameters[] = new DocumentingReflectionParameter(
array($class, $this->getName()),
$parameter->getName()
);
}
return $parameters;
}
This method first determines the declaring class that was stored at construction and checks if
it is an object or a string. Since you need a string for the next step, determine the object’s type
with get_class().
Following that, the parent’s getParameters() method is called. This will get you an array of
ReflectionParameter objects, but not DocumentingReflectionParameter objects. The whole
purpose of this function is to invoke the extended documenting form rather than the native form.
To test the getParameters() override, run the code in Listing 7-18.
Listing 7-18. Using getParameters (test2.php)
require_once('DocumentingReflection.php');
class demo {
/**



  • @param mixed $param1 The first comment.

  • @param string $param2 The second comment.
    */
    public function demoMethod($param1, $param2) {}
    }
    $reflector = new DocumentingReflectionMethod('demo', 'demoMethod');
    foreach($reflector->getParameters() as $param) {
    echo $param->getName(). ' ';
    echo $param->getType(). ' ';
    echo $param->getComment();
    echo "\n";
    }
    param1 mixed The first comment.
    param2 string The second comment.
    So, now you have the methods and parameters worked out. What about classes?
    DocumentingReflectionClass is the next class you need to create. Create this class as shown in
    Listing 7-19. and place the code in your DocumentingReflection.php file.
    McArthur_819-9C07.fm Page 94 Friday, February 22, 2008 8:59 AM

Free download pdf