class A
{
public $one = '';
public $two = '';
//Constructor
public function __construct($a='aa', $b=0)
{
//Constructor
}
//print variable one
public function echoOne()
{
echo $this->one."\n";
}
//print variable two
public function echoTwo()
{
echo $this->two."\n";
}
}
$a = new A();
$reflector = new ReflectionClass('A');
$properties = $reflector->getProperties();
// print_r($properties);
// Array
// (
// [0] => ReflectionProperty Object
// (
// [name] => one
// [class] => A
// )
// [1] => ReflectionProperty Object
// (
// [name] => two
// [class] => A
// )
// )
$methods = $reflector->getMethods();
// print_r($methods);
// Array
// (
// [0] => ReflectionMethod Object
// (
// [name] => __construct
// [class] => A
// )
// [1] => ReflectionMethod Object
// (
// [name] => echoOne
// [class] => A
// )
// [2] => ReflectionMethod Object
// (
// [name] => echoTwo
// [class] => A
// )
// )
$constructs = $reflector->getConstructor();
// print_r($constructs);
// ReflectionMethod Object
// (
// [name] => __construct
// [class] => A
// )
$constuctParameters = $constructs->getParameters();
//!!注意construct里的$a,$b如果没给默认值,必须在在给完construct赋值后才能使用,否则报错。
// print_r($constuctParameters);
// Array
// (
// [0] => ReflectionParameter Object
// (
// [name] => a
// )
// [1] => ReflectionParameter Object
// (
// [name] => b
// )
// )
foreach ($constructs->getParameters() as $param) {
if ($param->isDefaultValueAvailable()) {
echo $param->getDefaultValue();echo "<hr>";
} else {
echo "无默认值,<hr>";
}
}
//aa
//0
//====更多反射机制,任意框架(如:yii2)下任意代码处 new \Reflection();然后点击Reflection进去,就可以看到该类下所有方法
//---更多例子:https://m.jb51.net/show/105663
php5反射机制
猜你喜欢
转载自blog.csdn.net/wuhuagu_wuhuaguo/article/details/80675532
今日推荐
周排行