Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1

(^100) CHAPTER 7 ■ REFLECTION API
//If only a single attribute
if(is_string($rawAttributes)) {
$rawAttributes = array($rawAttributes);
}
foreach($rawAttributes as $attribute) {
//Parse attribute
$tmp = explode(' ',$attribute, 2);
$type = $tmp[0];
$data = isset($tmp[1])?$tmp[1]:null;
/
Create an attribute class instance by taking
the attribute name and adding the string
'Attribute' to the end. Thus an attribute
WebServiceMethod becomes a class
WebServiceMethodAttribute
/
$rc = new ReflectionClass($type. 'Attribute');
$instance = $rc->newInstance($data);
//Associate the ReflectionMethod with the attribute
$instance->setMethod($this);
$attributes[] = $instance;
unset($instance, $rc, $type, $data, $tmp);
}
return $attributes;
}
//Return an empty array if there are no attributes
return array();
}
Next, as shown in Listing 7-23, create two new classes for your Attribute types: an abstract
class called Attribute and a specialization of that class called WebServiceMethodAttribute.
Listing 7-23. Adding Classes for Attribute Types (Attributes.php)
<?PHP
abstract class Attribute {
protected $method;
McArthur_819-9C07.fm Page 100 Friday, February 22, 2008 8:59 AM

Free download pdf