Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP如何从变量中获取和使用name函数?_Php_Function_Variables - Fatal编程技术网

PHP如何从变量中获取和使用name函数?

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 )

正如我们所看到的,我们从$\u get获得name函数,然后检查这个类中的exist函数。但当我们无法打印函数名时,就会遇到问题,因为我们不知道如何正确地打印
$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));
}