Home >Backend Development >PHP Tutorial >php中利用反照访问类私有方法

php中利用反照访问类私有方法

WBOY
WBOYOriginal
2016-06-13 11:16:16750browse

php中利用反射访问类私有方法

?

class aa {

private function echoP($name){

return $name;

}

?

private function echoMM($aa, $bb){

return array_merge($aa ,$bb);

}

}

?

?

$class = new ReflectionClass('aa');

? $method = $class->getMethod('echoP');

? $method->setAccessible(true);

? $re = $method->invokeArgs(new aa(), array('wei'));

? var_dump($re);

?

?

?

$mm = new ReflectionMethod('aa', 'echoMM');

$mm->setAccessible(true);

var_dump($mm->invokeArgs(new aa(), array(array(1), array(2))));

?

可用于单测中

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn