PHP code example of gres / dim

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

    

gres / dim example snippets


use Dim\Container;
use Dim\Service;

class One { /* ... */ }

class Two { /* ... */ }

class Foo
{
    public function __construct(One $one, Two $two, $three)
    {
        // ...
    }
    // ...
}

// Instantiates the container
$container = new Container;
// Puts service that creates an instance of "One" to the container
$container->set(new Service('One'));
// Puts instance of "Two" to the container
$container->set(new Two);
// Puts service that creates an instance of "Foo" to the container
$container->set(new Service('Foo'));
// ...
// Instantiates "Foo" passing dependencies "One", "Two" and third argument "3" to the constructor
$three = $container->get('Foo', array('three' => 3));

    {
        "res/dim": "1.*"
        }
    }
    

    
    

    $container = new Dim\Container;
    

$container->set('value' , 'name');
// or
$container->set('value' , array('name1', 'name2'));
// or
$container->name = 'value';
// or
$container['name'] = 'value';

$container->set(new Service('Foo'));
// or
$container->set(new Service('Foo') , array('name1', 'name2'));
// or
$container->Foo = new Service('Foo');
// or
$container['Foo'] = new Service('Foo');

$container->set(
    new Service('Foo', array(new One, new Two, 3))
);
// or
$container->set(
    new Service('Foo', array('one' => new One, 'two' => new Two, 'three' => 3))
);
// or
$container->set(
    new Service('Foo', array(0 => new One, 'two' => new Two, 2 => 3))
);
// ...

$container->Foo = new Service('Foo');
$container->alias('Foo', 'alias');
// or
$container->alias('Foo', array('alias1', 'alias2'));

$foo = $container->get('Foo');
// or
$foo = $container->Foo;
// or
$foo = $container['Foo'];
// or
$foo = $container('Foo');

$foo = $container->get('Foo', array('three' => 'three'));
// or
$foo = $container('Foo', array('three' => 'three'));

$container->scope('foo')->set(new Service('One'));
$container->scope('foo')->set(new Two);
$container->scope('foo')->set(new Service('Foo'));
// Service "Foo" will look for dependencies in scope "foo"
$foo1 = $container->scope('foo')->Foo;
// ...
$container->scope('bar')->set(new Service('One'));
$container->scope('bar')->set(new Two);
$container->scope('bar')->set(new Service('Foo'));
// Service "Foo" will look for dependencies in scope "bar"
$foo1 = $container->scope('bar')->Foo;

$container->scope('foo', function () use ($container) {
    $container->set(new Service('One'));
    $container->set(new Two);
    $container->set(new Service('Foo'));
});

$container->foo = new Service('Foo');
$foo1 = $container->foo;
$foo2 = $container->foo;

$container->foo = new Singleton('Foo');
$foo1 = $container->foo;
$foo2 = $container->foo;

$container->foo = new Factory('Foo', function () {
    return new Foo;
});
$foo = $container->foo;

$container->foo = new Service('Foo');
$container->bar = new Extension($container->foo, function (Foo $foo) {
    $foo->property = 'value';
    $foo->callMethod();
    // ...
    return $foo;
});
$bar = $container->bar;

$container->foo = new Service('Foo');
$foo = $container->raw('foo');

$container->foo = new Service('Foo');
$foo = $container->has('foo');
// or
$foo = isset($container['foo']);
// or
$foo = isset($container->foo);
// ...
$bar = $container->has('bar');

$container->remove('foo');
// or
unset($container['foo']);
// or
unset($container->foo);

$container->clear();