PHP code example of berlioz / service-container

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

    

berlioz / service-container example snippets


use Berlioz\ServiceContainer\Container;

$container = new Container();
$service = $container->add(MyService::class, 'alias'); // Returns a Berlioz\ServiceContainer\Service\Service object

use Berlioz\ServiceContainer\Container;
use Berlioz\ServiceContainer\Service\Service;

$container = new Container();
$service = new Service(MyService::class, 'alias');
$container->addService($service);

use Berlioz\ServiceContainer\Instantiator;

$instantiator = new Instantiator();
$object = $instantiator->newInstanceOf(
    MyClass::class,
    [
        'argument1' => 'Value',
        'argument3' => 'Value',
        'argument2' => 'Value'
    ]
);

use Berlioz\ServiceContainer\Instantiator;

$instantiator = new Instantiator();
$instantiator->invokeMethod(
    $myObject,
    'myMethodName',
    [
        'argument1' => 'Value',
        'argument3' => 'Value',
        'argument2' => 'Value'
    ]
);

use Berlioz\ServiceContainer\Instantiator;

$instantiator = new Instantiator();
$instantiator->invokeFunction(
    'myFunctionName',
    [
        'argument1' => 'Value',
        'argument3' => 'Value',
        'argument2' => 'Value'
    ]
);

use Berlioz\ServiceContainer\Container;

$container = new Container();
$container->call(fn() => 'test'); // Call closure
$container->call(MyClass::class); // Instantiate the class
$container->call(MyClass::method); // Call static method or instantiate the class and call method 

use Berlioz\ServiceContainer\Container;
use Berlioz\ServiceContainer\Inflector\Inflector;

$inflector = new Inflector(
    MyInterface::class, // Interface implemented by object
    'setFoo', // Method to call
    [/*...*/] // Arguments
);
$container = new Container();
$container->addInflector($inflector);

use Berlioz\ServiceContainer\Container;
use Berlioz\ServiceContainer\Provider\AbstractServiceProvider;

class MyServiceProvider extends AbstractServiceProvider
{
    // Declare services class and alias
    protected array $provides = [stdClass::class, 'service'];
    
    public function boot(Container $container) : void
    {
        // This method is called when provider is added to container.
        // Add inflectors here.
    }
    
    public function register(Container $container) : void
    {
         // Add services here
         $container->add(stdClass::class, 'service');
    }
}

use Berlioz\ServiceContainer\Container;

$container = new Container();
$container->addProvider(new MyServiceProvider());

$container->has('service'); // Returns TRUE
$container->get('service'); // Returns an `stdClass` instance

  Service::public function __construct(
      string|object $class,
      ?string $alias = null,
      callable|string|null $factory = null,
      ?CacheStrategy $cacheStrategy = null,
  )