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

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

WBOY
Release: 2016-06-13 12:52:45
Original
793 people have browsed it

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

?

可用于单测中

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template