PHP code example of castor / container

1. Go to this page and download the library: Download castor/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/ */

    

castor / container example snippets




$container = Castor\Container::boot();
$container->register('foo', fn() => new Foo());
$foo = $container->get('foo');



use Psr\Container\ContainerInterface;

$container = Castor\Container::boot();
$container->register('foo', static function (ContainerInterface $container) {
    $bar = $container->get(Bar::class);
    return new Foo($bar);
});
$foo = $container->get('foo');



$container = Castor\Container::boot();
$container->provide(function (Castor\Container $container) {
    $container->register('foo', fn() => new Foo());
});
$foo = $container->get('foo');



use Psr\Container\ContainerInterface;

$container = Castor\Container::boot();
$container->register('foo', fn() => new Foo());
$container->inflect('foo', function (Foo $foo, ContainerInterface $container) {
    $foo->setBar($container->get(Bar::class));
});
$foo = $container->get('foo');



use Psr\Container\ContainerInterface;

$container = Castor\Container::boot();
$container->register('foo', fn() => new Foo());
$container->inflect('foo', function (Foo $foo, ContainerInterface $container) {
    return new FooBar($foo, $container->get(Bar::class));
});
$foo = $container->get('foo'); // instance of FooBar



$container = Castor\Container::boot();
// Foo will be instantiated using reflection when a service Foo is requested.
$container->register(Foo::class);
// Foo will be instantiated when a FooInterface service is requested.
$container->register(FooInterface::class, Foo::class);



$container = Castor\Container::boot();
// Foo will be automatically instantiated using reflection.
$container->get(Foo::class);



$container = Castor\Container::boot(4); // This flag only enables caching.




$container = Castor\Container::boot();
$container->register(Foo::class);
$container->alias(Foo::class, 'foo');



$container = Castor\Container::boot();
$container->register(Foo::class);
$container->tag('dummy_services', Foo::class, Bar::class);