Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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方法_Php_Variables_Methods - Fatal编程技术网

调用名称为变量的PHP方法

调用名称为变量的PHP方法,php,variables,methods,Php,Variables,Methods,我试图从一个“Content”类调用几个方法,即 $content .= Content::page_contact_us(); 除了页面,我们可以联系任何东西 我试过了 $method = 'Content::page_contact_us()'; $content .= $$method_name; 而$content为空…您可以使用以下语法调用变量函数: $func = 'foo'; $func(); // This calls foo() 或者,在您的情况下:

我试图从一个“Content”类调用几个方法,即

$content .= Content::page_contact_us();
除了页面,我们可以联系任何东西

我试过了

$method = 'Content::page_contact_us()';

$content .= $$method_name; 

$content
为空…

您可以使用以下语法调用变量函数:

$func = 'foo';
$func();        // This calls foo()

或者,在您的情况下:

$method = 'Content::page_contact_us';
$content .= $method(); 

可以使用以下语法调用变量函数:

$func = 'foo';
$func();        // This calls foo()

或者,在您的情况下:

$method = 'Content::page_contact_us';
$content .= $method(); 

方法名称可以是变量,其方式与:


在中还有更多示例。

方法名称可以是变量,其方式与以下方法相同:


在中还有更多的例子。

您可以使用PHP的
调用用户函数()来实现这一点。
。以下是方法:

<?php
    class Content{
        public static function whatever(){
            return "This is a response from whatever Method inside the Content Class...";
        }
    }


    $method     = 'whatever';
    $content    = "";
    $content   .= Content::whatever();

    $content2   = call_user_func(array('Content', $method));

    var_dump($content);
    var_dump($content2);

     //RESPECTIVELY DISPLAYS::              
     'This is a response from whatever Method inside the Content Class...' (length=67)
     'This is a response from whatever Method inside the Content Class...' (length=67)

您可以使用PHP的
call\u user\u func()
实现这一点。以下是方法:

<?php
    class Content{
        public static function whatever(){
            return "This is a response from whatever Method inside the Content Class...";
        }
    }


    $method     = 'whatever';
    $content    = "";
    $content   .= Content::whatever();

    $content2   = call_user_func(array('Content', $method));

    var_dump($content);
    var_dump($content2);

     //RESPECTIVELY DISPLAYS::              
     'This is a response from whatever Method inside the Content Class...' (length=67)
     'This is a response from whatever Method inside the Content Class...' (length=67)

调用用户函数($method\u name)
怎么样?但是从变量声明中删除
()
。帮助页面有一些内容。
内容:$method\u name()
调用用户函数($method\u name)
怎么样?但是从变量声明中删除
()
。帮助页面有一些内容。
内容:$method\u name()
,或者,在您的例子中:
在php<7.0中。请查看AD7six的答案,以获得更好的方法。使用@Machavity可以更好地处理变量函数和类调用。不,它们不是。继续使用
$method()
,但改用
$method=['Content','page\u contact\u us']
或者,在您的情况下:
在php<7.0中。查看AD7six的答案,了解更好的方法。使用@Machavity可以更好地处理变量函数和类调用。不,它们不是。继续使用
$method()
,但改用
$method=['Content','page\u contact\u us']
。$Content.=Content::$method\u name();工作很好…谢谢你。。。但只是为了完成-$content.=call_user_func(['content',$method_name]);这导致了以下问题:致命错误:不在对象上下文中使用$this。。。。在数据库实用程序函数中…
在数据库实用程序函数中
这与您的问题无关。不详细说明。。。这是完全相同的方法。。。只是用了两种不同的方式。。。这两种方法都应该是静态的,但从另一个角度看似乎不等价。。。有什么区别无论你在看什么,都与问题无关,当你不在对象中时,你(直接或间接地)调用了
$this->something
。如果需要,请调查并提出另一个问题,该问题与此问题无关。$content.=content::$method_name();工作很好…谢谢你。。。但只是为了完成-$content.=call_user_func(['content',$method_name]);这导致了以下问题:致命错误:不在对象上下文中使用$this。。。。在数据库实用程序函数中…
在数据库实用程序函数中
这与您的问题无关。不详细说明。。。这是完全相同的方法。。。只是用了两种不同的方式。。。这两种方法都应该是静态的,但从另一个角度看似乎不等价。。。有什么区别无论你在看什么,都与问题无关,当你不在对象中时,你(直接或间接地)调用了
$this->something
。如果需要,请调查并提出另一个问题,这与此问题无关。