如何使用call_user_func进行静态类方法?

B S*_*ven 7 php reflection callback

以下代码工作正常.

LibraryTests::TestGetServer();
Run Code Online (Sandbox Code Playgroud)

获取LibraryTests中的函数数组并运行它们:

$methods = get_class_methods('LibraryTests');
foreach ($methods as $method) {
  call_user_func('LibraryTests::' . $method . '()' );
}
Run Code Online (Sandbox Code Playgroud)

这会引发错误: Warning: call_user_func(LibraryTests::TestGetServer()) [function.call-user-func]: First argument is expected to be a valid callback

这是被调用的类:

class LibraryTests extends TestUnit {

    function TestGetServer() {
        TestUnit::AssertEqual(GetServer(), "localhost/");
    }
    .
    .
    .
Run Code Online (Sandbox Code Playgroud)

怎么修?

在PHP 5.2.8中工作.

hak*_*kre 13

要么(从PHP 5.2.3开始):

$methods = get_class_methods('LibraryTests');
foreach ($methods as $method) {
  call_user_func('LibraryTests::' . $method);
}
Run Code Online (Sandbox Code Playgroud)

或者(早些时候):

$methods = get_class_methods('LibraryTests');
foreach ($methods as $method) {
  call_user_func(array('LibraryTests', $method));
}
Run Code Online (Sandbox Code Playgroud)

请参阅call_user_func文档回调伪类型的文档.