反射api
(2011-11-06 11:20:36)
PHP 5中带有一个完整的反射API,增加了逆向工程中的类,接口,功能和方法,以及扩展的能力。此外,反射API还提供了检索功能,类和方法的文档注释的方法。
<?php
function counter()
{
static $c = 0;
return $c++;
}
//
Create an instance of the ReflectionFunction
class $func =
new ReflectionFunction('counter');
//
Print out basic information printf(
"===>
The %s function '%s'\n".
"
declared in %s\n".
"
lines %d to %d\n",
$func->isInternal()
? 'internal' : 'user-defined',
$func->getName(),
$func->getFileName(),
$func->getStartLine(),
$func->getEndline()
);
//
Print documentation comment printf("--->
Documentation:\n %s\n", var_export($func->getDocComment(), 1));
//
Print static variables if existant if
($statics = $func->getStaticVariables())
{
printf("--->
Static variables: %s\n", var_export($statics, 1));
}
//
Invoke the function printf("--->
Invokation results in: ");
var_dump($func->invoke());
//
you may prefer to use the export() method echo "\nReflectionFunction::export()
results:\n";
echo ReflectionFunction::export('counter');
?>
|
喜欢
0
赠金笔
加载中,请稍候......