PHP code example of okeyaki / pimple-di

1. Go to this page and download the library: Download okeyaki/pimple-di 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/ */

    

okeyaki / pimple-di example snippets


class Container extends \Pimple\Container
{
    use \Okeyaki\Pimple\DiTrait;
}

$container = new Container();

class Foo
{
}

class Bar
{
    private $foo;

    public function __construct(Foo $foo)
    {
        $this->foo = $foo;
    }

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

$foo = $container[Foo::class];

$bar = $contaienr[Bar::class];
$bar->foo();

class Foo
{
}

$container['foo'] = function () {
    return new Foo();
};

$container->bind(Foo::class, 'foo');

$foo = $container[Foo::class];

class Foo
{
}

class Bar
{
    private $foo;

    public function __construct(Foo $foo)
    {
        $this->foo = $foo;

        $this->name = $name;
    }

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

$bar = $container->make(Bar::class);
$bar->foo();

class Bar
{
    private $foo;

    private $baz;

    public function __construct(Foo $foo, $baz)
    {
        $this->foo = $foo;

        $this->baz = $baz;
    }

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

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

$bar = $container->make(Bar::class, [
    'baz' => 'baz',
]);

$bar->foo();
$bar->baz();