【问题标题】:How do I dynamically invoke a class method in PHP? [duplicate]如何在 PHP 中动态调用类方法?
【发布时间】:2010-09-21 08:26:10
【问题描述】:

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

call_user_func(...)

仅适用于静态函数?

谢谢。

【问题讨论】:

    标签: php callback


    【解决方案1】:

    它是双向的——你需要使用正确的语法

    //  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)
    
    【解决方案2】:

    选项 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 比使用上面列出的任一选项慢几个数量级。

    【讨论】:

      【解决方案3】:

      你的意思是这样的?

      <?php
      
      class A {
          function test() {
              print 'test';
          }
      }
      
      $function = 'test';
      
      // method 1
      A::$function();
      
      // method 2
      $a = new A;    
      $a->$function();
      
      ?>
      

      【讨论】:

        【解决方案4】:

        从 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 会自动调用它。

        【讨论】:

          【解决方案5】:
          call_user_func(array($object, 'methodName'));
          

          更多详情,请参阅php callback documentation.

          【讨论】:

            【解决方案6】:

            编辑:我刚刚弄清楚了您要问的问题...啊好吧..无论如何都会留下我的cmets。如果你愿意,你可以用变量替换类和方法的名称..(但你疯了) - 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,因为没有类的实例。 (这是他们走得更快的原因之一。)

            【讨论】:

              【解决方案7】:

              这可能是有用的替代品

              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);
              

              【讨论】:

              • 这个想法可以通过使用接口来进一步改进,即ICallbackContainer。
              猜你喜欢
              • 1970-01-01
              • 2010-09-20
              • 2014-07-12
              • 1970-01-01
              • 1970-01-01
              • 2023-03-16
              • 2011-01-07
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多