PHP code example of m-r-r / lazy-container

1. Go to this page and download the library: Download m-r-r/lazy-container library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

m-r-r / lazy-container example snippets




use LazyContainer\Container;

class Foo
{
    public $name;

    function __construct($name)
    {
        echo "Instance of Foo created with name \"$name\".\n";
    }
}

class Bar
{
    public $foo;
    function __construct(Container $c)
    {
        echo "Instance of Bar created.\n";
        $this->foo = $c->foo;
    }
}


class Baz
{
    public $foo;
    public $bar;

    function __construct(Container $c)
    {
        echo "Instance of Baz created.\n";
        $this->bar = $c->bar;
        $this->foo = $c->foo;
    }
}


$cont = new Container;

$cont->foo = function($self, $cfg) {
    return new Foo($self->fooName);
};

$cont->bar = function($self) {
    return new Bar($self);
};

$cont->baz = function($self) {
    return new Baz($self);
};

$cont->fooName = 'Quux';

// Later:
var_dump($cont->baz instanceOf Baz
         && $cont->baz->bar === $cont->bar
         && $cont->baz->foo === $cont->bar->foo);