©
本文档使用
php.cn手册 发布
(PHP 5 >= 5.3.2)
ReflectionMethod::setAccessible — 设置方法是否访问
$accessible
)设置方法是否可以访问,例如通过设置可以访问能够执行私有方法和保护方法
accessible
可以访问设置 TRUE
,否则设置 FALSE
没有返回值。
[#1] dave1010 at gmail dot com [2011-05-11 03:21:25]
This is handy for accessing private methods but remember that things are normally private for a reason! Unit Testing is one (debatable) use case for this.
Example:
<?php
class Foo {
private function myPrivateMethod() {
return 7;
}
}
$method = new ReflectionMethod('Foo', 'myPrivateMethod');
$method->setAccessible(true);
echo $method->invoke(new Foo);
// echos "7"
?>
This works nicely with PHPUnit: http://php.net/manual/en/reflectionmethod.setaccessible.php