PHP code example of phespro / container

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

    

phespro / container example snippets




$container = new Container();
$container->add('your-service-id', fn() => new YourService, Type::SCOPED);

$service1 = $scope->get('your-service-id');
$scope = $container->newScope();
$service2 = $scope->get('your-service-id');
$service3 = $scope->get('your-service-id');

// $service1 === $service2 -> false -> different instance
// $service1 === $service3 -> false -> different instance
// $service2 === $service3 -> true -> same instance because it's the same scope



iner = new Container;

$container->add('some_id', fn(Container $c) => new MyService); // register singleton
$container->addFactory('other_id', fn(Container $c) => new OtherService); // register factory
$container->add('tagged_service', fn(Container $c) => new TaggedService,  ['console_command']);

$container->decorate('some_id', fn(Container $c, callable $prev) => new Decorator($prev());

// or decorate it with factory
$container->decorateWithFactory('some_id', fn(Container $c, callable $prev) => new Decorator($prev()));