PHP code example of omarphp / di

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

    

omarphp / di example snippets


use Omar\DependencyInjection\Container;

class RockBand {}

$container = Container::create();

$band = $container->get(RockBand::class);
assert($band instanceof RockBand);

use Omar\DependencyInjection\Container;

class Guitarist {}

class Drummer {}

class RockBand
{
    public function __construct(Guitarist $guitarist, Drummer $drummer) {}
}

$container = Container::create();

$band = $container->get(RockBand::class);
assert($band instanceof RockBand);

use Omar\DependencyInjection\Config;
use Omar\DependencyInjection\Container;

interface Guitarist {}
abstract class Drummer {}

class JimmyPage implements Guitarist {}
class JohnBonham extends Drummer {}

$config = Config::init()
    ->bind(Guitarist::class, JimmyPage::class)
    ->bind(Drummer::class, JohnBonham::class);

$container = Container::create($config);

$guitarist = $container->get(Guitarist::class);
$drummer = $container->get(Drummer::class);

assert($guitarist instanceof JimmyPage);
assert($drummer instanceof JohnBonham);

use Omar\DependencyInjection\Config;
use Omar\DependencyInjection\Container;

interface Guitarist {}
abstract class Drummer {}

class JimmyPage implements Guitarist {}
class JohnBonham extends Drummer {}

class RockBand
{
    public function __construct(Guitarist $guitarist, Drummer $drummer) {}
}

$config = Config::init()
    ->bind(Guitarist::class, JimmyPage::class)
    ->bind(Drummer::class, JohnBonham::class);

$container = Container::create($config);

$band = $container->get(RockBand::class);
assert($band instanceof RockBand);

use Omar\DependencyInjection\Config;
use Omar\DependencyInjection\Container;
use Omar\DependencyInjection\Setup;

interface Guitarist {}
interface Drummer {}

class JimmyPage implements Guitarist {}
class George implements Guitarist {}
class JohnBonham implements Drummer {}
class Ringo implements Drummer {}

abstract class RockBand
{
    public function __construct(Guitarist $guitarist, Drummer $drummer) {}
}

class LedZeppelin extends RockBand {}
class Beatles extends RockBand {}

$config = Config::init()
    ->setup(LedZeppelin::class, Config::params()
        ->wire('guitarist', JimmyPage::class)
        ->wire('drummer', JohnBonham::class)
    )
    ->setup(Beatles::class, Config::params()
        ->wire('guitarist', George::class)
        ->wire('drummer', Ringo::class)
    );

$container = Container::create($config);

$ledzep = $container->get(LedZeppelin::class);
assert($ledzep instanceof LedZeppelin);

$beatles = $container->get(Beatles::class);
assert($beatles instanceof Beatles);

use Omar\DependencyInjection\Config;
use Omar\DependencyInjection\Container;
use Omar\DependencyInjection\Setup;

class Guitarist
{
    public function __construct(string $guitarType, int $bornInYear) {}
}

$config = Config::init()
    ->setup(Guitarist::class, Config::params()
        ->config('guitarType', 'Les Paul')
        ->config('bornInYear', 1944)
    );

$container = Container::create($config);

$jimmyPage = $container->get(Guitarist::class);
assert($jimmyPage instanceof Guitarist);

use Omar\DependencyInjection\Config;
use Omar\DependencyInjection\Container;
use Omar\DependencyInjection\Setup;

interface Guitar {}
class LesPaul implements Guitar {}

class Guitarist
{
    public function __construct(Guitar $guitar, int $bornInYear) {}
}

$config = Config::init()
    ->setup(Guitarist::class, Config::params()
        ->wire('guitar', LesPaul::class)
        ->config('bornInYear', 1944)
    );

$container = Container::create($config);

$jimmyPage = $container->get(Guitarist::class);
assert($jimmyPage instanceof Guitarist);

use Omar\DependencyInjection\Config;
use Omar\DependencyInjection\Container;

interface Guitar {}
class LesPaul implements Guitar {}

class Guitarist
{
    public function __construct(Guitar $guitar, int $bornInYear) {}
}

$config = Config::init()
    ->bind(Guitar::class, LesPaul::class)
    ->provider(Guitarist::class, function (Guitar $guitar) {
        return new Guitarist($guitar, 1944);
    });

$container = Container::create($config);

$jimmyPage = $container->get(Guitarist::class);
assert($jimmyPage instanceof Guitarist);

use Omar\DependencyInjection\Config;
use Omar\DependencyInjection\Container;

interface Guitar {}
class LesPaul implements Guitar {}

class Guitarist
{
    public function __construct(Guitar $guitar) {}
}

interface Drummer {}
class JohnBonham implements Drummer {}

class RockBand
{
    public function __construct(Guitarist $guitarist, Drummer $drummer) {}
}

class RockBandFactory
{
    /** @var Drummer */
    private $drummer;

    public function __construct(Drummer $drummer) {
        $this->drummer = $drummer;
    }

    public function __invoke(Guitar $guitar): RockBand
    {
        return new RockBand(new Guitarist($guitar), $this->drummer);
    }
}

$config = Config::init()
    ->bind(Drummer::class, JohnBonham::class)
    ->bind(Guitar::class, LesPaul::class)
    ->factory(RockBand::class, RockBandFactory::class);

$container = Container::create($config);

$band = $container->get(RockBand::class);
assert($band instanceof RockBand);