PHP code example of mattferris / di

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

    

mattferris / di example snippets


use MattFerris\Di\Di;

$di = new Di();

// register 'FooService'
$di->set('FooService', return new \FooService());
});

// register `BarService` using type-hinted argument
$di->set('FooService', function (Di $container) {
    return new \FooService($container);
});

$fooService = $di->get('FooService');

$di->set('FooService', function ($di) {
    return new \FooService();
}, Di::NON_SINGLETON);

// manually retrieve an instance of FooService
$di->set('BarService', function ($di) {
    $fooService = $di->get('FooService');
    return new \BarService($fooService);
});

// inject an instance of FooService
$di->set('BarService', function (\FooService $fooService) {
    return new \BarService($fooService);
});

if ($di->has('FooService')) {
    echo 'Yay!';
}

$services = $di->find('FTP.');

foreach ($services as $key => $service) {
    $service->saveFile($file);
}

array(
    'FTP.host1' => [instance of \FtpService],
    'FTP.host2' => [instance of \FtpService]
)

$di->set('FooService', function (\Bar\Service\Class $bar) {
    return new \FooService($bar);
});

class MyProvider extends MattFerris\Di\ServiceProvider
{
    public function provides($consumer)
    {
        // validate consumer is an instance of \MattFerris\Di\ContainerInterface
        parent::provides($consumer);

        $di->set('MyService', function () { ... });
    }
}

$di->register(new MyProvider());

$myService = $di->get('MyService');

$di->setParameter('db', array(
    'user' => 'joe',
    'pass' => 'p4ssw0rd',
    'host' => 'localhost',
    'db' => 'foo'
));

$di->set('DB', function ($di) {
    $cfg = $di->getParameter('db');
    return new \DbConnection(
        $cfg['user'], $cfg['pass'], $cfg['host'], $cfg['db']
    );
}, true);

$di->delegate('Foo.', $container);

class FooClass
{
    public function __construct($argA, $argB)
    {
    }
}

$object = $di->injectConstructor(
    'FooClass',
     array('argA' => 'foo', 'argB' => 'bar')
);

$object = $di->injectConstructor(
    'FooClass',
    array('blah' => 'bling', 'argB' => 'bar', 'argA' => 'foo')
);

$result = $di->injectMethod($object, 'someMethod', array('foo' => 'bar'));

$result = $di->injectStaticMethod('FooClass', 'someMethod', array('foo' => 'bar'));

$result = $di->injectFunction('someFunction', array('foo' => 'bar'));

$result = $di->injectFunction($closure, array('foo' => 'bar'));

$object = $di->injectConstructor(
    'FooClass',
    array('argA' => $di->get('FooService'), 'argB' => $di->getParameter('foo'))
);

$object = $di->injectConstructor(
    'FooClass',
    array('argA' => '%FooService', 'argB' => ':foo')
);

$object = $di->injectConstructor(
    'FooClass',
    array('argA' => '\Foo\Service\Class')
);