[英]Php: how to make private static method to public, and class cannot be insantized?
abstract class MyClass
{
private static makeMePublic()
{
}
}
I want to make MyClass::makeMePublic method to be callable from the outside. 我想让MyClass :: makeMePublic方法可以从外部调用。 I saw a solution here: Best practices to test protected methods with PHPUnit but that requires the class to be instantized.
我在这里看到了一个解决方案: 使用PHPUnit测试受保护方法但需要对类进行实例化的最佳实践 。 In this case its not possible.
在这种情况下,它是不可能的。 So, how to make "public" this method?
那么,如何制作“公开”这种方法呢?
The docs say you can just pass null
as the first param to invokeArgs
to execute a static method. 文档说你可以将
null
作为第一个param传递给invokeArgs
来执行静态方法。
protected static function getMethod($name) {
$class = new ReflectionClass('MyClass');
$method = $class->getMethod($name);
$method->setAccessible(true);
return $method;
}
public function testMakeMePublic() {
$foo = self::getMethod('makeMePublic');
$foo->invokeArgs(null, $args);
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.