php PHP中的动态类方法调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/251485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dynamic class method invocation in PHP
提问by VirtuosiMedia
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:
有没有办法为PHP动态调用同一个类中的方法?我没有正确的语法,但我希望做类似的事情:
$this->{$methodName}($arg1, $arg2, $arg3);
回答by andy.gurin
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
您甚至可以使用反射 api http://php.net/manual/en/class.reflection.php
回答by Konrad Rudolph
Just omit the braces:
只需省略大括号:
$this->$methodName($arg1, $arg2, $arg3);
回答by RodolfoNeto
You can use the Overloading in PHP: Overloading
您可以使用 PHP 中的重载: 重载
class Test {
private $name;
public function __call($name, $arguments) {
echo 'Method Name:' . $name . ' Arguments:' . implode(',', $arguments);
//do a get
if (preg_match('/^get_(.+)/', $name, $matches)) {
$var_name = $matches[1];
return $this->$var_name ? $this->$var_name : $arguments[0];
}
//do a set
if (preg_match('/^set_(.+)/', $name, $matches)) {
$var_name = $matches[1];
$this->$var_name = $arguments[0];
}
}
}
$obj = new Test();
$obj->set_name('Any String'); //Echo:Method Name: set_name Arguments:Any String
echo $obj->get_name();//Echo:Method Name: get_name Arguments:
//return: Any String
回答by Peter Bailey
You can also use call_user_func()and call_user_func_array()
您还可以使用call_user_func()和call_user_func_array()
回答by Noah Goodrich
If you're working within a class in PHP, then I would recommend using the overloaded __call function in PHP5. You can find the reference here.
如果您在 PHP 的类中工作,那么我建议使用 PHP5 中的重载 __call 函数。您可以在此处找到参考。
Basically __call does for dynamic functions what __set and __get do for variables in OO PHP5.
基本上 __call 对动态函数的作用就像 __set 和 __get 对 OO PHP5 中的变量所做的那样。
回答by Snapey
Still valid after all these years! Make sure you trim $methodName if it is user defined content. I could not get $this->$methodName to work until I noticed it had a leading space.
这么多年了还有效!如果 $methodName 是用户定义的内容,请确保对其进行修剪。我无法让 $this->$methodName 工作,直到我注意到它有一个前导空格。
回答by user46637
In my case.
就我而言。
$response = $client->{$this->requestFunc}($this->requestMsg);
Using PHP SOAP.
使用 PHP SOAP。
回答by David
You can store a method in a single variable using a closure:
您可以使用闭包将方法存储在单个变量中:
class test{
function echo_this($text){
echo $text;
}
function get_method($method){
$object = $this;
return function() use($object, $method){
$args = func_get_args();
return call_user_func_array(array($object, $method), $args);
};
}
}
$test = new test();
$echo = $test->get_method('echo_this');
$echo('Hello'); //Output is "Hello"
EDIT: I've edited the code and now it's compatible with PHP 5.3. Another example here
编辑:我已经编辑了代码,现在它与 PHP 5.3 兼容。这里的另一个例子

