<?php
class Person
{
public $name;
private $age;
protected $hobbies = [];
public function sleep(){
return '';
}
protected function eat(){
return '';
}
private function play_game(){
return '';
}
}
$person = new Person();
$method = new ReflectionClass($person);
$res = $method->getMethods();
echo "<pre/>";
print_r($res);
/*Array
(
[0] => ReflectionMethod Object
(
[name] => sleep
[class] => Person
)
[1] => ReflectionMethod Object
(
[name] => eat
[class] => Person
)
[2] => ReflectionMethod Object
(
[name] => play_game
[class] => Person
)
)*/
?>PHP利用反射感知类中成员方法
最新推荐文章于 2024-07-28 15:40:41 发布
本文介绍了一个简单的PHP程序,演示了如何使用ReflectionClass类获取Person类的所有方法,包括public、protected和private方法,并通过实例化ReflectionMethod对象显示这些方法的名称。
9953

被折叠的 条评论
为什么被折叠?



