Dynamic class method invocation in PHP

后端 未结 8 1942
感动是毒
感动是毒 2020-11-28 05:05

Is there a way to dynamically invoke a method in the same class for PHP? I don\'t have the syntax right, but I\'m looking to do something similar to this:

$t         


        
8条回答
  •  粉色の甜心
    2020-11-28 05:24

    There is more than one way to do that:

    $this->{$methodName}($arg1, $arg2, $arg3);
    $this->$methodName($arg1, $arg2, $arg3);
    call_user_func_array(array($this, $methodName), array($arg1, $arg2, $arg3));
    

    You may even use the reflection api http://php.net/manual/en/class.reflection.php

提交回复
热议问题