如何访问函数存储在变量中的类的PHP函数?
例如:
$functionname = "say_hello";
$obj = new Class();
$obj -> $functionname();发布于 2012-01-19 14:57:52
如果函数名存储在变量中,请检查它是否存在且可调用,如下所示
if ( method_exists( $product, $method ) ) {
print $product->$method(); // invoke the method
}然后检查它是否可调用。
if ( is_callable( array( $product, $method) ) ) {
print $product->$method(); // invoke the method
}发布于 2012-01-19 14:51:56
使用callback
快速示例:array($obj, $functionname)
call_user_func(array($obj, $functionname), your parameters);有关上述参考链接的完整说明
发布于 2012-01-19 15:58:10
private function __get($name) {
$method = 'get'.$name;
return $this->$method();
}编写构建方法名称并调用它的__get函数
https://stackoverflow.com/questions/8922053
复制相似问题