PHP code example of yarfox / container
1. Go to this page and download the library: Download yarfox/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.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
yarfox / container example snippets
abstract class AA {}
class AAI extends AA{}
class A {
}
class B {
public function __construct(A $a)
{
}
}
class C {
public function __construct(B $b, int $a = 123)
{
}
}
class D {
public function __construct(A $a, AA $b)
{}
}
$container = Container::instance();
$container->registerProducer('a', function () {
return new A();
});
$container->resolve('a'); // get A()
$container->resolve(A::class); // get A()
$container->resolve(C::class); // get C()
$container->resolve(D::class); // throw Container Exception: AA is not instantiable!
$container->registerInstance(AA::class, $container->resolveClass(AAI::class)); // AA::class => AAI()
$container->resolve(D::class); // get D()