PHP code example of neunerlei / container-delegate
1. Go to this page and download the library: Download neunerlei/container-delegate 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/ */
neunerlei / container-delegate example snippets
use Neunerlei\ContainerDelegate\Container;
$container = new Container();
use Neunerlei\ContainerDelegate\Container;
class A {}
class B {
public $a;
public function __construct(A $a) {$this->a = $a;}
}
$container = new Container();
$i = $container->get(B::class);
use Neunerlei\ContainerDelegate\Container;
interface AInterface {}
class A implements AInterface {
}
$container = new Container();
$i = $container->get(AInterface::class);
var_dump($i instanceof A);
use Neunerlei\ContainerDelegate\Container;
class A {
public function __construct(string $foo = "bar", ?Container $container = null) {
var_dump($foo === "bar");
var_dump($container instanceof Container);
}
}
$container = new Container();
$i = $container->get(A::class);
use Neunerlei\ContainerAutoWiringDeclaration\SingletonInterface;
use Neunerlei\ContainerDelegate\Container;
class A implements SingletonInterface {
}
$container = new Container();
$i = $container->get(A::class);
var_dump($i instanceof A);
var_dump($i === $container->get(A::class));
use Neunerlei\ContainerAutoWiringDeclaration\InjectableInterface;
use Neunerlei\ContainerDelegate\Container;
class A {}
class B implements InjectableInterface {
public $a;
public function injectA(A $a){
$this->a = $a;
}
}
$container = new Container();
$i = $container->get(B::class);
var_dump($i->a instanceof A);
use Neunerlei\ContainerDelegate\Container;
interface AInterface {
public function foo();
}
class A implements AInterface {
public function foo(){
return "foo!";
}
}
class B {
public $a;
public function __construct(AInterface $lazyA){
$this->a = $lazyA;
}
}
$container = new Container();
$i = $container->get(B::class);
var_dump($i->a instanceof AnInterface);
var_dump($i->a instanceof A); // This is FALSE!
var_dump($i->a->foo());
use Neunerlei\ContainerDelegate\Container;
class A {}
class B {
public $a;
public function __construct(A $a) {$this->a = $a;}
}
$container = new Container("/path/to/cache/directory");
$i = $container->get(B::class);
use Neunerlei\ContainerDelegate\Adapter\LeagueContainerAdapter;
class A {}
class B {
public $a;
public function __construct(A $a) {$this->a = $a;}
}
// Create the compound container
$container = new \League\Container\Container();
// Create and register the delegated container
$delegate = new LeagueContainerAdapter("/path/to/cache/directory");
$container->delegate($delegate);
// Make sure the delegate container knows it's parent
$delegate->setContainer($container);
$delegate->setLeagueContainer($container);
// Done, start requesting your stuff :)
$i = $container->get(B::class);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.