Php:如何将私有静态方法公开,并且类不能被解释?

Joh*_*ith 3 php reflection

abstract class MyClass
{
    private static makeMePublic()
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

我想让MyClass :: makeMePublic方法可以从外部调用.我在这里看到了一个解决方案:使用PHPUnit测试受保护方法但需要对类进行实例化的最佳实践.在这种情况下,它是不可能的.那么,如何制作"公开"这种方法呢?

mpe*_*pen 10

文档说你可以null作为第一个参数传递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);
  ...
}
Run Code Online (Sandbox Code Playgroud)