58

如何在 PHP 中动态调用类方法?类方法不是静态的。看起来

call_user_func(...)

仅适用于静态函数?

谢谢。

4

7 回答 7

99

它适用于两种方式 - 您需要使用正确的语法

//  Non static call
call_user_func( array( $obj, 'method' ) );

//  Static calls
call_user_func( array( 'ClassName', 'method' ) );
call_user_func( 'ClassName::method' ); // (As of PHP 5.2.3)
于 2008-11-07T18:53:18.797 回答
29

选项1

// invoke an instance method
$instance = new Instance();
$instanceMethod = 'bar';
$instance->$instanceMethod();

// invoke a static method
$class = 'NameOfTheClass';
$staticMethod = 'blah';
$class::$staticMethod();

选项 2

// invoke an instance method
$instance = new Instance();
call_user_func( array( $instance, 'method' ) );

// invoke a static method
$class = 'NameOfTheClass';
call_user_func( array( $class, 'nameOfStaticMethod' ) );
call_user_func( 'NameOfTheClass::nameOfStaticMethod' ); // (As of PHP 5.2.3)

选项 1 比选项 2 快,因此请尝试使用它们,除非您不知道要传递给该方法的参数数量。


编辑:以前的编辑器在清理我的答案方面做得很好,但删除了与 call_user_func 不同的 call_user_func_array 的提及。

PHP有

mixed call_user_func ( callable $callback [, mixed $parameter [, mixed $... ]] ) 

http://php.net/manual/en/function.call-user-func.php

mixed call_user_func_array ( callable $callback , array $param_arr )

http://php.net/manual/en/function.call-user-func-array.php

使用 call_user_func_array 比使用上面列出的任一选项慢几个数量级。

于 2008-11-07T22:59:13.183 回答
17

你的意思是这样吗?

<?php

class A {
    function test() {
        print 'test';
    }
}

$function = 'test';

// method 1
A::$function();

// method 2
$a = new A;    
$a->$function();

?>
于 2008-11-07T18:54:57.717 回答
6

从 PHP7 开始,您使用类似数组的方式:

// Static call only
[TestClass::class, $methodName](...$args);

// Dynamic call, static or non-static doesn't matter
$instance = new TestClass;
[$instance, $methodName](...$args);

只需将类名替换为TestClass,方法名替换为$methodName,方法参数替换为...$args。请注意,在后一种情况下,方法是静态还是非静态都没有关系;PHP 会自动调用它。

于 2019-07-17T12:35:36.410 回答
2
call_user_func(array($object, 'methodName'));

有关更多详细信息,请参阅php 回调文档。

于 2008-11-07T18:53:04.493 回答
1

编辑:我刚刚弄清楚你想问什么......嗯......无论如何都会留下我的评论。如果你愿意,你可以用变量替换类和方法的名称......(但你疯了) - nick


要从类中调用函数,您可以使用以下两种方法之一...

您可以创建类的实例,然后调用它。例如:

$bla = new Blahh_class();
$bla->do_something();

或者......您可以静态调用该函数......即没有类的实例。例如:

Blahh_class::do_something()

当然你需要声明你的函数是静态的:

class Blahh_class {   
    public static function do_something(){
        echo 'I am doing something';
    }
}

如果一个类没有被定义为静态的,那么你必须创建一个对象的实例..(所以对象需要一个构造函数)例如:

class Blahh_class {
    $some_value;

    public function __construct($data) {
        $this->$some_value = $data;
    }

    public function do_something() {
         echo $this->some_value;
    }
}

要记住的重要一点是静态类函数不能使用$this,因为没有类的实例。(这是他们走得更快的原因之一。)

于 2008-11-10T02:27:22.420 回答
0

这可能是有用的替代品

class ReferenceContainer {

    function __construct(CallbackContainer $callbackContainer) {

        //Alternatively you can have no parameters in this constructor and create a new instance of CallbackContainer and invoke the callback in the same manner        
        //var_dump($this->callbackContainer);
        $data = 'This is how you parse a class by reference';
        $callbackContainer->myCallback($data);

    }

}

class CallbackContainer {

    function __construct() {}

    function myCallback($data) {

        echo $data."\n";

    }

}

$callbackContainer = new CallbackContainer();
$doItContainer = new ReferenceContainer($callbackContainer);
于 2016-05-03T05:12:43.383 回答