Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.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 - Fatal编程技术网

Php 将变量传递给方法中包含的文件

Php 将变量传递给方法中包含的文件,php,Php,我找不到这个问题的答案。我有一个用PHP编写的类。PHP类方法中包含的文件似乎不会继承方法范围内的变量 class Classname { public function template_editor (){ $GLOBALS['template_editor'] = true; $editable=false; $GLOBALS['editing'] = false; $template_html = $this-&

我找不到这个问题的答案。我有一个用PHP编写的类。PHP类方法中包含的文件似乎不会继承方法范围内的变量

class Classname {
    public function template_editor (){
        $GLOBALS['template_editor'] = true; 
        $editable=false;
        $GLOBALS['editing'] = false;
        $template_html =  $this->thetemplate (array('editable'=>$editable));
        include (TEMPLATE_DIR_URI."/inc/template_editor.php");
    }
}
我希望变量$template_html被传递到包含的文件中,以便我可以回显它。我编写了许多函数,函数中声明的所有变量都传递到包含的文件中,但在类方法中不起作用

在我目前拥有的包含文件中

<?php echo  $template_html;  ?>

…还有一些html和Jquery。我希望所有方法变量都能在包含的文件中使用,以便在包含的文件中使用它们。

来自:

如果包含发生在调用文件中的函数内,则 被调用文件中包含的所有代码的行为都将与 已在该函数中定义。因此,它将跟随变量 该职能的范围。这个规则的一个例外是魔法常数 在include发生之前由解析器对其进行评估

您的问题很可能是此行没有为
$template\u html

$template_html =  $this->thetemplate (array('editable'=>$editable));
或者您在template_editor.php中错误地引用了变量

像这样检查
$template\u html
的值可以让您了解原因:

class Classname {
    public function template_editor (){
        $GLOBALS['template_editor'] = true; 
        $editable=false;
        $GLOBALS['editing'] = false;
        $template_html =  $this->thetemplate (array('editable'=>$editable));

        // Add this for debugging
        if (is_empty($template_html)) {
            error_log('$template_html is empty.');
        }

        include (TEMPLATE_DIR_URI."/inc/template_editor.php");
    }
}

我不明白。解决方案就在代码中,两次。为什么不将它用于
$template\u html
?$template\u html将在
template\u DIR\u URI中提供。“/inc/template\u editor.php”
-您确定在这里设置了它吗?该包含的文件还应该能够访问$this->thetemplate()和任何其他方法—即使使用受保护的访问—我有一个使用该方法的工作站点。当使用include时,php将代码包含在文件中,就好像它是内联的一样。它在哪里?我觉得很奇怪。当我尝试在包含的文件template_editor.php中回显变量$template_html时,没有输出。我可以在方法中很好地回显变量,但不能在文件中回显变量。在回显$template_html的位置是否有任何条件(if/else)逻辑?不,我不只是回显条件语句;那么我所拥有的代码应该确实有效?PHP应该在包含的文件中提供这些变量?我可以使用该方法回显$template_html,但不能在包含的文件中回显。您是否可以更新您的问题,以包含包含文件中回显变量的相关部分?