PHP利用反射感知类中成员方法

<?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
        )

)*/

?>


猜你喜欢

转载自blog.csdn.net/yan4413/article/details/80624239