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);
});
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');