1. Go to this page and download the library: Download kelvinmo/lightcontainer 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/ */
kelvinmo / lightcontainer example snippets
class A {}
class B {
public function __construct(A $a) {}
}
class C {
public function __construct(B $b) {}
}
$c = new C(new B(new A()));
$container = new LightContainer\Container();
$c = $container->get(C::class);
interface FooInterface {}
class FooInterfaceImpl implements FooInterface {}
class FooInterfaceSubclass extends FooInterfaceImpl {}
class D {
public function __construct(FooInterface $i) {}
}
$container->set(D::class)->alias(FooInterface::class, FooInterfaceImpl::class);
$d = $container->get(D::class);
// This is equivalent to new D(new FooInterfaceImpl())
$container->set(D::class)
->alias(FooInterface::class, FooInterfaceImpl::class)
->alias(FooInterfaceImpl::class, FooInterfaceSubclass::class);
$d = $container->get(D::class);
// This is equivalent to new D(new FooInterfaceSubclass())
class F {}
class G {
public function __construct(F $f, string $host, int $port = 80) {}
}
$container->set(G::class)->args('example.com', 8080);
// Multiple calls also work
$container->set(G::class)
->args('example.com')
->args(8080);
// Optional parameters can be omitted
$container->set(G::class)->args('example.com');
class H {
// PHP 8 is struct(F $f, int|string $bar, A $a, $baz) {}
}
// This sets $bar to 'one' and $baz to 'two'
$container->set(H::class)->args('one', 'two');
class I {
/**
* @param FooInterface $foo
*/
public function __construct($foo) {}
}
// See $foo to the FooInterfaceImpl from the container (which may be
// instantiated if r\Container::ref('@foo'));
class J {
public function setA(A $a) {}
}
$container->set(J::class)->call('setA');
class K {
public function setFoo(FooInterface $f, bool $debug);
}
$container->set(K::class)
->alias(FooInterface::class, FooInterfaceImpl::class)
->call('setFoo', false);
class Modifier implements InstanceModifierInterface {
public function modify(object $obj, LightContainerInterface $container): object {}
}
class ModifyMe {}
$modifier = new Modifier();
$container->set(ModifyMe::class)->modify($modifier);
// The container will call $modifier->modify() before returning the instance
$modify_me = $container->get(ModifyMe::class);
$container->set(A::class)->shared();
// $a1 and $a2 are the same instance
$a1 = $container->get(A::class);
$a2 = $container->get(A::class);
#[Shared]
class A {}
$container->set(A::class);
// This is equivalent to:
// $container->set(A::class)->shared();
class L {}
class M extends L {}
class N extends M {}
$container->set(L::class)->shared();
// $n1 and $n2 are the same instance because
// N inherited 'shared' from L
$n1 = $container->get(N::class);
$n2 = $container->get(N::class);
$container->set(L::class)->shared()->propagate(false);
// $n1 and $n2 are the different instances because
// L does not propagate its options to autowired
// subclasses
$n1 = $container->get(N::class);
$n2 = $container->get(N::class);
#[Propagate(false)]
class L {}
// Set 'shared' to true for all autowired resolvers
$container->set('*')->shared();
class FooInterfaceImplGlobal implements FooInterface {}
class FooInterfaceImplForD implements FooInterface {}
$container->set(FooInterface::class, FooInterfaceImplGlobal::class);
$container->set(D::class)->alias(FooInterface::class, FooInterfaceImplForD::class);
// D uses FooInterfaceImplForD instead of FooInterfaceImplGlobal
$container->get(D::class);
class O {
public function __construct(\PDO $db) {}
}
$container->set('@prod_db', \PDO::class)
->args('mysql:host=example.com;dbname=prod');
$container->set('@dev_db', \PDO::class)
->args('mysql:host=example.com;dbname=dev');
$container->set(O::class)->alias(\PDO::class, '@prod_db');
interface ImplementedInterface {}
class InterfaceImplementation implements ImplementedInterface {}
interface UnimplementedInterface {}
class P1 {
function __construct(?ImplementedInterface $i) {}
}
class P2 {
function __construct(ImplementedInterface $i = null) {}
}
class Q1 {
function __construct(?UnimplementedInterface $i) {}
}
class Q2 {
function __construct(UnimplementedInterface $i = null) {}
}
$container->set(ImplementedInterface::class, InterfaceImplementation::class);
// $p1->i and $p2->i will both contain a InterfaceImplementation,
// as an alias is registered for ImplementedInterface and the aliased class
// exists
$p1 = $container->get(P1::class);
$p2 = $container->get(P2::class);
// $q1->i and $q2->i will both be null, as there is no alias registered for
// UnimplementedInterface
$q1 = $container->get(Q1::class);
$q2 = $container->get(Q2::class);
$container->set(P1::class)->alias(ImplementedInterface::class, null);
$container->set(P2::class)->alias(ImplementedInterface::class, null);
// $p1->i and $p2->i are now both null
$p1 = $container->get(P1::class);
$p2 = $container->get(P2::class);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.