PHP中的动态类方法调用 [英] Dynamic class method invocation in PHP
本文介绍了PHP中的动态类方法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有一种方法可以在PHP的同一类中动态调用方法?我的语法不正确,但是我正在寻找类似的方法:
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:
$this->{$methodName}($arg1, $arg2, $arg3);
推荐答案
有多种方法可以做到这一点:
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));
您甚至可以使用反射API http://php.net/manual/en /class.reflection.php
You may even use the reflection api http://php.net/manual/en/class.reflection.php
这篇关于PHP中的动态类方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文