Prev | Current Page 109 | Next

Hasin Hayder

"Object-Oriented Programming with PHP5"

The first three of
these are self explanatory; let's look at the fourth one which is invoked. This is a nice
example taken from the PHP Manual:
class Counter
{
private static $c = 0;
/**
* Increment counter
*
* @final
* @static
* @access public
* @return int
*/
final public static function increment()
{
return ++self::$c;
}
}
Chapter 5
[ 105 ]
// Create an instance of the Reflection_Method class
$method = new ReflectionMethod('Counter', 'increment');
// Print out basic information
printf(
"===> The %s%s%s%s%s%s%s method '%s' (which is %s)\n" .
" declared in %s\n" .
" lines %d to %d\n" .
" having the modifiers %d[%s]\n",
$method->isInternal() ? 'internal' : 'user-defined',
$method->isAbstract() ? ' abstract' : '',
$method->isFinal() ? ' final' : '',
$method->isPublic() ? ' public' : '',
$method->isPrivate() ? ' private' : '',
$method->isProtected() ? ' protected' : '',
$method->isStatic() ? ' static' : '',
$method->getName(),
$method->isConstructor() ? 'the constructor' :
'a regular method',
$method->getFileName(),
$method->getStartLine(),
$method->getEndline(),
$method->getModifiers(),
implode(' ', Reflection::getModifierNames(
$method->getModifiers()))
);
// Print documentation comment
printf("---> Documentation:\n %s\n",
var_export($method->getDocComment(), 1));
// Print static variables if existant
if ($statics= $method->getStaticVariables()) {
printf("---> Static variables: %s\n", var_export($statics, 1));
}
// Invoke the method
printf("---> Invokation results in: ");
var_dump($method->invoke(NULL));
?>
When executed, this code will give the following output:
===> The user-defined final public static method 'increment' (which is
a regular method)
declared in PHPDocument1
lines 14 to 17
having the modifiers 261[final public static]
Reflection and Unit Testing
[ 106 ]
---> Documentation:
'/**
* Increment counter
*
* @final
* @static
* @access public
* @return int
*/'
---> Invokation results in: int(1)
ReflectionParameter
Another very important object in the reflection family is ReflectionParameter.


Pages:
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121