丢失包含文件中对象的范围

Am trying to call an object's method inside of an included file which the included file itself, was loaded in the class with output buffer like below:

public function render($file){

    ob_start();
    require_once($file);
    $this->template = ob_get_contents();
    ob_end_clean();

    return $this->template;
}

Getting these errors:

Notice: Undefined variable: template
Fatal error: Call to a member function showTitle() on a non-object

The included file is to be used as a template and there will be many. It works fine when used as static property and method set, but not with the instantiated object's property and method set which is the required way.

Below are all test files for your review.

Class file "classTest.php":

<?php
    // classTest.php

    class Test{

    protected $template;
    protected $title =  "Title";
    static    $titleStatic = "Title Static";

    public function render($file){

        ob_start();
        require_once($file);
        $this->template = ob_get_contents();
        ob_end_clean();

        return $this->template;
    }

    public function showTitle(){
        return $this->title;
    }

    static function showTitleStatic(){
        return self::$titleStatic;

    }
}
?>

Test file "test.php":

<?php
    // test.php

    require_once 'classTest.php';

    $template = new Test;

    echo $template->showTitle();

    $templateFile = 'includeTest.php';

    echo $template->render($templateFile);

?>

Include file "includeTest.php":

<!--    includeTest.php     -->

<div id="container">
    <div id="titleStatic"><?php echo Test::showTitleStatic(); ?></div>
    <div id="title"><?php echo $template->showTitle(); ?></div>
</div>

展开翻译

译文

我试图在一个包含文件中调用一个对象的方法,其中包含的文件本身已在类中加载 输出缓冲区如下:</ p>

 公共函数渲染($ file){

ob_start();
require_once($ file);
$ this-&gt; template = ob_get_contents();
ob_end_clean();

返回$ this-&gt;模板;
}
</ code> </ pre>

获取这些错误:</ p>

 注意:未定义的变量:template 
Fatal error:在非对象上调用成员函数showTitle() </ pre>

包含的文件将用作模板,并且会有很多。 当用作 static </ code>属性和方法集时,它可以正常工作,但不能与实例化对象的属性和方法集一起使用,这是必需的方法。</ p>

以下是所有测试 文件供您查看。</ p>

类文件“classTest.php”:</ p>

 &lt;?php 
// classTest.php \ n
class Test {

protected $ template;
protected $ title =“Title”;
static $ titleStatic =“Title Static”;

公共函数render($ file){
\ n ob_start();
require_once($ file);
$ this-&gt; template = ob_get_contents();
ob_end_clean();

返回$ this-&gt;模板;
}

\ n公共函数showTitle(){
返回$ this-&gt; title;
}

静态函数showTitleStatic(){
return self :: $ titleStatic;

}
}
? &gt;
</ code> </ pre>

测试文件“test.php”:</ p>

 &lt;?php 
// test .php

require_once'classTest.php';

$ template = new Test;

echo $ template-&gt; showTitle( );

$ templateFile ='includeTest.php';

echo $ template-&gt; render($ templateFile);

?&gt;
</ code> </ pre>

包含文件“includeTest.php”:</ p>

 &lt;! -  includeTest.php  - &gt; 

&lt; div id =“container” &gt;
&lt; div id =“titleStatic”&gt;&lt;?php echo Test :: showTitleStatic(); ?&gt;&lt; / div&gt;
&lt; div id =“title”&gt;&lt;?php echo $ template-&gt; showTitle(); ?&gt;&lt; / div&gt;
&lt; / div&gt;
</ code> </ pre>
</ div>

1个回答

Change your includeTest.php to this:

<div id="container">
    <div id="titleStatic"><?php echo Test::showTitleStatic(); ?></div>
    <div id="title"><?php echo $this->showTitle(); ?></div>
</div>

Your calling $template when it should be $this since it's being populated inside the class not from outside.

展开翻译

译文



将includeTest.php更改为:</ p>

 &lt; div id =“  container“&gt; 
&lt; div id =”titleStatic“&gt;&lt;?php echo Test :: showTitleStatic(); ?&gt;&lt; / div&gt;
&lt; div id =“title”&gt;&lt;?php echo $ this-&gt; showTitle(); ?&gt;&lt; / div&gt;
&lt; / div&gt;
</ code> </ pre>

您的调用$模板应该是$ this,因为它在类中而不是来自 外面。</ p>
</ div>

dongshi8425
dongshi8425 谢谢。 这个有效。 优秀! +1
接近 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问