Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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; (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.

I haven't coded PHP in a while so please help me out here.


2 Answers

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

like image 108
Touki Avatar answered Jan 21 '26 03:01

Touki


Simply: By calling include/require inside of an method in a class.

File A.php:

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

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

File B.php:

<html>
    <body><?php echo $this->test; ?></body>
</html>
like image 41
eisberg Avatar answered Jan 21 '26 04:01

eisberg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!