Pro PHP- Patterns, Frameworks, Testing and More

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

Listing 7-16. Experimenting with DocumentingReflection (Experiment.php)

require_once('DocumentingReflection.php');

class demo {

/**
* @param string $param this is the comment
*/
public function demoMethod($param='test') {}

}

$refparam = new DocumentingReflectionParameter(
array('demo', 'demoMethod'),
'param'
);

var_dump($refparam->getComment());
var_dump($refparam->getType());

You should see the following output:

string(19) "this is the comment"
string(6) "string"

Now, normally you don’t access parameters by providing that much information. Let’s
modify the DocumentingReflectionMethod class to override the getParameters() function, making it
return DocumentingReflectionParmeter[] instead of ReflectionParameter[]. Include the code
in Listing 7-17 in the DocumentingReflectionMethod class.

Listing 7-17. Overriding getParameters (DocumentingReflection.php)

public function getParameters() {
$parameters = array();

if(is_object($this->_declaringClass)) {
$class = get_class($this->_declaringClass);
} else if(is_string($this->_declaringClass)) {
$class = $this->_declaringClass;
}

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

Free download pdf