如何在PHP中动态调用类方法?

如何在PHP中动态调用类方法?,php,callback,Php,Callback,如何在PHP中动态调用类方法?类方法不是静态的。看来 call_user_func(...) 仅适用于静态函数 谢谢 call_user_func(array($object, 'methodName')); 有关更多详细信息,请参见它是双向工作的-您需要使用正确的语法 // Non static call call_user_func( array( $obj, 'method' ) ); // Static calls call_user_func( array( 'ClassNa

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

call_user_func(...)
仅适用于静态函数

谢谢

call_user_func(array($object, 'methodName'));

有关更多详细信息,请参见

它是双向工作的-您需要使用正确的语法

//  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)
你是说像这样

<?php

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

$function = 'test';

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

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

?>

选项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数组不同

PHP拥有

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


使用call\u user\u func\u数组比使用上面列出的任何一个选项都要慢几个数量级。

EDIT:我刚刚了解了你想问的问题。。。啊,好吧。。无论如何我都会留下我的评论。如果你喜欢,你可以用变量替换类和方法的名称…(但你疯了)-nick


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

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

或者。。。您可以静态调用该函数。。i、 e.没有类的实例。 e、 g:

当然,您确实需要声明您的函数是静态的:

class Blahh_class {   
    public static function do_something(){
        echo 'I am doing something';
    }
}
如果类未定义为静态,则必须创建该对象的实例。。(因此对象需要一个构造函数) e、 g:


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

我发现最好的办法就是

call_user_func_array(array(__NAMESPACE__ .'\Foo', 'test'), array('Philip'));

它的工作就像一个魅力

这可能是有用的替代品

Class Foo{
 public function show(){
  echo 'I am in Foo Class show method';
 }

}

call_user_func(array('Foo', 'show'));

$classname = new Foo;
call_user_func(array($classname, 'show'));
call_user_func($classname .'::show'); // As of 5.2.3

$foo = new Foo();    
call_user_func(array($foo, 'show'));
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);

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

//仅静态调用
[TestClass::class,$methodName](…$args);
//动态调用,静态或非静态都无所谓
$instance=newtestclass;
[$instance,$methodName](…$args);

只需将类名替换为
TestClass
,方法名替换为
$methodName
,方法参数替换为
..$args
。注意,在后一种情况下,方法是静态的还是非静态的并不重要;PHP将自动调用它。

这些:和这个:这个想法可以通过使用接口(即ICallbackContainer)进一步改进。
class Blahh_class {
    $some_value;

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

    public function do_something() {
         echo $this->some_value;
    }
}
call_user_func_array(array(__NAMESPACE__ .'\Foo', 'test'), array('Philip'));
Class Foo{
 public function show(){
  echo 'I am in Foo Class show method';
 }

}

call_user_func(array('Foo', 'show'));

$classname = new Foo;
call_user_func(array($classname, 'show'));
call_user_func($classname .'::show'); // As of 5.2.3

$foo = new Foo();    
call_user_func(array($foo, 'show'));
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);