1. Go to this page and download the library: Download oukhennicheabdelkrim/dic 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/ */
oukhennicheabdelkrim / dic example snippets
oukhennicheabdelkrim\DIC\DIC;
$container = new DIC();
$container = new DIC();
//Bind myFoo to the Foo instance
$container->bind('myFoo',function(){
return new Foo();
});
$myFoo = $container->get('myFoo'); // Foo instance
$container = new DIC();
$container->bind('myFoo',function(){
return new Foo();
});
var_dump( $container->get('myFoo') === $container->get('myFoo')); // true
$container = new DIC();
$container->bind('myFoo',function(){
return new Foo();
});
$container->get('myFoo') === $container->getFactory('myFoo') // false
$container->getFactory('myFoo') === $container->getFactory('myFoo') // false
/* Foo class */
class Foo{
public $i;
public function __construct($i = 44){
$this->i=$i;
}
}
/* Bar class */
class Bar{
public $foo;
public function __construct(Foo $foo){
$this->foo=$foo;
}
}
$container = new DIC();
$bar=$container->get('Bar');
var_dump($bar->foo->i); // 44
// get method always returns a singleton instance.
var_dump($bar->foo === $container->get('Foo')); // true
//getFactory method always returns a new instance.
var_dump($bar->foo === $container->getFactory('Foo')); // false
$container = new DIC();
$container->bind('bar1',function($container){
// resolve Foo using the class name
return new Bar($container->get('Foo'));
});
$bar1 = $container->get('bar1');// a singleton bar1 (Bar instance)
$bar = $container->get('Bar'); // a singleton Bar
var_dump($bar1 === $bar); // false
var_dump($bar1->foo === $bar->foo) ; // true
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.