简体   繁体   English

锂PHP框架如何在PHP页面中生成$ this上下文?

[英]How does the lithium PHP framework generate a $this context in a PHP page?

I've been investigating the Lithium PHP Framework and I don't understand how it sets $this->context; 我一直在研究Lithium PHP Framework ,我不明白它是如何设置$this->context; ( particularly in this layout .) 特别是在这种布局中 。)

Since you cannot simply re-assign $this obviously this layout will get included at some point and what confuses me even more is the fact that they use $this outside a class definition. 既然你不能简单地重新分配$this这个布局会在某些时候被包含在内,更令我困惑的是他们在类定义之外使用$this

I haven't coded PHP in a while so please help me out here. 我有一段时间没有编写PHP,所以请在这里帮助我。

The first idea that strikes me is that this templating page is called from a method. 让我印象深刻的第一个想法是,从一个方法调用这个模板页面。

class Viewer
{
    public $html;
    private $title;
    private $content;

    public function __construct()
    {
        $this->html = new \Utilities\HTMLBag();
    }
    public function loadView($template)
    {
        ob_start();
        include 'path/to/views/'.$template.'.php';
        $this->content = ob_get_clean();
    }
    public function title()
    {
        return $this->title;
    }
}

From this point, the included $template can access any method of Viewer's class 从这一点来说,包含的$template可以访问Viewer类的任何方法

Simply: By calling include/require inside of an method in a class. 简单地说:通过调用类中的方法中的include / require。

File A.php: 文件A.php:

<?php
class A {
    public $test = 'Hello';

    public function xyz() {
        include 'B.php';
    }
}

File B.php: 文件B.php:

<html>
    <body><?php echo $this->test; ?></body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM