PHP如何从变量中获取和使用name函数?
正如我们所看到的,我们从$\u get获得name函数,然后检查这个类中的exist函数。但当我们无法打印函数名时,就会遇到问题,因为我们不知道如何正确地打印PHP如何从变量中获取和使用name函数?,php,function,variables,Php,Function,Variables,正如我们所看到的,我们从$\u get获得name函数,然后检查这个类中的exist函数。但当我们无法打印函数名时,就会遇到问题,因为我们不知道如何正确地打印$this->function() 有人知道怎么做吗?首先,您需要使用方法\u exists,而不是属性\u exists。然后,您可以使用call\u user\u func调用该方法: $function = $_GET["function"]; if(property_exists($this, $function )
$this->function()
有人知道怎么做吗?首先,您需要使用
方法\u exists
,而不是属性\u exists
。然后,您可以使用call\u user\u func
调用该方法:
$function = $_GET["function"];
if(property_exists($this, $function )){
echo $this->function(); // problem is here
}
查看PHP的
call_user_func
或echo$this->$function()代码>或使用谢谢,我没有早点找到它($function()
和关于func)。请打印答案$function()
对我的工作会更好。为什么我不应该使用属性?it函数可以检查类中是否存在变量和函数。对不起,我认为你的说法不对。下面是一个快速测试:很抱歉,正如我们现在看到的(在您的示例和手动php.net中)函数property\u exists
check exist property only,它函数不检查类中的exist方法。非常感谢。附言:我很遗憾,但我的名誉不允许投票表决。
$function = $_GET["function"];
if(method_exists($this, $function )){
echo call_user_func(array($this, $function));
}