如何使用call_user_func的静态类方法? [英] How to use call_user_func for static class method?

查看:722
本文介绍了如何使用call_user_func的静态类方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码工作正常。

LibraryTests::TestGetServer();

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

Get the array of functions in LibraryTests and run them:

$methods = get_class_methods('LibraryTests');
foreach ($methods as $method) {
  call_user_func('LibraryTests::' . $method . '()' );
}

这会抛出一个错误:警告:call_user_func :: TestGetServer())[function.call-user-func]:第一个参数是一个有效的回调

正在调用的类:

class LibraryTests extends TestUnit {

    function TestGetServer() {
        TestUnit::AssertEqual(GetServer(), "localhost/");
    }
    .
    .
    .

如何解决?

PHP 5.2.8。

Working in PHP 5.2.8.

推荐答案

(从PHP 5.2.3开始):

Either (as of PHP 5.2.3):

$methods = get_class_methods('LibraryTests');
foreach ($methods as $method) {
  call_user_func('LibraryTests::' . $method);
}

或(早):

$methods = get_class_methods('LibraryTests');
foreach ($methods as $method) {
  call_user_func(array('LibraryTests', $method));
}

请参阅 call_user_func ­ Docs 以及回调伪类型 ­文档

这篇关于如何使用call_user_func的静态类方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆