PHP code example of slince / di

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

$container->register('bar', Acme\Bar::class);
$container->register('foo', Acme\Foo::class)
    ->addArgument(new Slince\Di\Reference('bar')); //refer to 'bar'
    
var_dump($container->get('bar') === $container->get('foo')->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->register(Acme\Foo::class);
$container->setAlias('foo-alias', Acme\Foo::class);
$foo = $container->get('foo-alias');

var_dump($foo instanceof Acme\Foo);      // true

$container->setDefaults([
    'share' => false
]);
$container->register('foo', Acme\Foo::class);
var_dump($container->get('foo') === $container->get('foo'));      // false

$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

$container->setParameters([
    'foo' => 'hello',
    'bar' => [
        'baz' => 'world'
    ]
]);

$container->register('bar', Acme\Bar::class)
     ->setArguments([
        'foo' => $container->getParameter('foo'),
        'baz' => $container->getParameter('bar.baz')
    ]);

$bar = $container->get('bar');
var_dump($bar->foo);  // hello
var_dump($bar->bar); // world

$container->register('foo')->addTag('my.tag', array('hello' => 'world'));

$serviceIds = $container->findTaggedServiceIds('my.tag');

foreach ($serviceIds as $serviceId => $tags) {
    foreach ($tags as $tag) {
        echo $tag['hello'];
    }
}