1. Go to this page and download the library: Download weew/container library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?phprequire_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
weew / container example snippets
$container = new Container();
$container->set('foo', 'bar');
// returns bar
$container->get('foo');
classFoo{}
// returns a new instance of Foo
$container->get(Foo::class);
classFoo{
publicfunction__construct($x, $y = 2){};
}
// parameters are matched by name
$container->get(Foo::class, ['x' => 1]);
classFoo{
publicfunction__construct(Bar $bar){}
}
classBar{}
// returns a new instance of Foo,// Foo's constructor receives a new instance of Bar
$container->get(Foo::class);
classFoo{}
$container->set(Foo::class, new Foo());
// or
$container->set(new Foo());
// everyone will get the same instance of Foo
$container->get(Foo::class);
classFoo{
public $bar;
}
classBar{}
// a factory method will get it's dependencies resolved too.
$container->set(Foo::class, function(Bar $bar){
$foo = new Foo();
$foo->bar = $bar;
return $foo;
});
$container->set('foo', 1);
// a container can be injected the same way as any other dependency
$container->set('bar', function(IContainer $container){
return $container->get('foo');
));
interfaceIFoo{}
classFooimplementsIFoo{}
$container->set(IFoo::class, Foo::class);
// will return an instance of Foo
$container->get(IFoo::class);
interfaceIFoo{}
classFooimplementsIFoo{}
$container->set(IFoo::class, new Foo());
// everyone will get the same instance of Foo
$container->get(IFoo::class);
interfaceIFoo{}
classFooimplementsIFoo{}
$container->set(IFoo::class, function(){
returnnew Foo();
});
// will return a new instance of Foo
$container->get(IFoo::class);
interfaceIFoo{}
classFooimplementsIFoo{}
classBar{
publicfunction__construct(IFoo $foo){}
}
$container->set(IFoo::class, Foo::class);
// returns an instance of Bar// Bar receives an instance of Foo, which implements the interface IFoo
$container->get(Bar::class);
classBar{}
functionfoo(Bar $bar, $foo){}
// method foo gets called and receives an instance of Bar// as with the other container methods, you can always pass your own arguments
$container->callFunction('foo', ['foo' => 1]);
classBar{}
// closure gets called and receives an instance of Bar
$container->callFunction(function(Bar $bar){});
classFoo{}
classBar{
publicfunctiontakeFoo(Foo $foo, $x){}
}
$bar = new Bar();
// method takeFoo gets invoked and receives a new instance// of Foo, as well as the custom arguments
$container->callMethod($bar, 'takeFoo', ['x' => 1]);
// you could also let the container create an instance
$container->callMethod(Bar::class, 'takeFoo', ['x' => 1]);
classFoo{}
classBar{
publicstaticfunctiontakeFoo(Foo $foo, $x){}
}
// method takeFoo gets invoked and receives a new instance// of Foo, as well as the custom arguments
$container->callStaticMethod(Bar::class, 'takeFoo', ['x' => 1]);
// same as $container->callFunction($functionName, $args)
$container->call($functionName, $args);
// same as $container->callFunction($closure, $args)
$container->call($closure, $args);
// same as $container->callMethod($instance, $method, $args)
$container->call([$instance, $method], $args);
// same as $container->callMethod($className, $method, $args)
$container->call([$className, $method], $args);
// same as $container->callStaticMethod($className, $staticMethod, $args)
$container->call([$className, $staticMethod], $args);
$container->set([MyImplementation::class, IImplementation::class], function(){
returnnew MyImplementation('foo');
});
// both calls will return a value from the same factory
$container->get(MyImplementation::class);
$container->get(IImplementation::class);
$container->set('foo', 'bar');
// will return true
$container->has('foo');
$container->set('foo', 'bar');
$container->remove('foo');
// will return false
$container->has('foo');
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.