1. Go to this page and download the library: Download slince/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/ */
slince / di example snippets
namespace Acme;
class Foo
{
/**
* @var \Acme\Bar
*/
public $bar;
/**
* Construct.
*/
public function __construct(Bar $bar)
{
$this->bar = $bar;
}
}
class Bar
{
public $foo;
public $baz;
public function __construct($foo, $baz)
{
$this->foo = $foo;
$this->baz = $baz;
}
}
$container = new Slince\Di\Container();
$container->register(Acme\Foo::class);
$foo = $container->get(Acme\Foo::class);
var_dump($foo instanceof Acme\Foo); // true
var_dump($foo->bar instanceof Acme\Bar); // true
class NewsletterManagerStaticFactory
{
public static function createNewsletterManager($parameter)
{
$newsletterManager = new NewsletterManager($parameter);
// ...
return $newsletterManager;
}
}
// call the static method
$container->register(
NewsletterManager::class,
array(NewsletterManagerStaticFactory::class, 'createNewsletterManager')
)->addArgument('foo');
// call a method on the specified factory service
$container->register(NewsletterManager::class, [
new Reference(NewsletterManagerFactory::class),
'createNewsletterManager'
]);
$container->setDefaults([
'autowire' => false,
]);
$container->register('foo', Acme\Foo::class)
->addArgument(new Acme\Bar()); // You have to provide $bar
var_dump($container->get('foo') instanceof Acme\Foo::class); // true