PHP code example of tobento / service-container

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

    

tobento / service-container example snippets


use Tobento\Service\Container\Container;

$container = new Container();

$has = $container->has(Foo::class);

$foo = $container->get(Foo::class);

use Tobento\Service\Container\Container;

class Foo
{
    public function __construct(
        private string $name
    ) {} 
}

$container = new Container();

$container->set(Foo::class, new Foo('value'));

$foo = $container->get(Foo::class);

use Tobento\Service\Container\Container;

class Foo
{
    public function __construct(
        private string $name
    ) {} 
}

$container = new Container();

// By the construct method:
$container->set(Foo::class)->construct('value');

// By the with method using parameter name:
$container->set(Foo::class)->with(['name' => 'value']);

// By the with method using parameter position:
$container->set(Foo::class)->with([0 => 'value']);

$foo = $container->get(Foo::class);

use Tobento\Service\Container\Container;

class Foo
{
    public function __construct(
        private string $name
    ) {} 
}

class Bar
{
    public function value(): string
    {
        return 'value';
    } 
}

$container = new Container();

$container->set(Foo::class, static function(Bar $bar) {
    return new Foo($bar->value());
});

$foo = $container->get(Foo::class);

$container->set(BarInterface::class, Bar::class);

use Tobento\Service\Container\Container;

class Foo
{
    public function index(Bar $bar, string $name) {} 
}

class Bar {}

$container = new Container();

$container->set(Foo::class)->callMethod('index', ['name' => 'value']);

$container->set(Foo::class)->callMethod('index', [1 => 'value']);

$foo = $container->get(Foo::class);

use Tobento\Service\Container\Container;

class Foo {}
class Bar {}

$container = new Container();

$container->set(Foo::class)->prototype();

$container->set(Bar::class, function() {
    return new Bar();
})->prototype();

var_dump($container->get(Foo::class) === $container->get(Foo::class));
// bool(false)

var_dump($container->get(Bar::class) === $container->get(Bar::class));
// bool(false)

use Tobento\Service\Container\Container;

class Foo
{
    public function __construct(
        private Bar $bar,
        private string $name
    ) {} 
}

class Bar {}

$container = new Container();

$foo = $container->make(Foo::class, ['name' => 'value']);

class Foo
{
    public function index(Bar $bar, string $name): string
    {
        return $name;
    } 
}

class Bar {}

$container = new Container();

$name = $container->call([Foo::class, 'index'], ['name' => 'value']);

use Tobento\Service\Container\Container;
use Tobento\Service\Container\ResolverInterface;

$container = new Container(new CustomResolver());

/**
 * ResolverInterface
 */
interface ResolverInterface
{    
    /**
     * Resolve the given identifier to a value.
     *
     * @param string $id Identifier of the entry.
     * @param array<int|string, mixed> $parameters
     *
     * @return mixed
     */
    public function resolve(string $id, array $parameters = []): mixed;

    /**
     * Resolve the given definition.
     *
     * @param DefinitionInterface $definition
     *
     * @return mixed The value of the resolved definition.
     */
    public function resolveDefinition(DefinitionInterface $definition): mixed;
    
    /**
     * If the given identifier is resolvable.
     *
     * @param mixed $id Identifier of the entry.
     * @return bool True if resolvable, otherwise false.
     */
    public function isResolvable(mixed $id): bool;
}