PHP code example of chubbyphp / chubbyphp-container

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

    

chubbyphp / chubbyphp-container example snippets




use App\Service\MyService;
use Chubbyphp\Container\MinimalContainer;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

$container = new MinimalContainer();
$container->factories([
    MyService::class => static function (ContainerInterface $container): MyService {
        return new MyService($container->get(LoggerInterface::class));
    },
]);



use App\Service\MyService;
use Chubbyphp\Container\MinimalContainer;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

$container = new MinimalContainer();

// new
$container->factory(MyService::class, static function (ContainerInterface $container): MyService {
    return new MyService($container->get(LoggerInterface::class));
});

// existing (replace)
$container->factory(MyService::class, static function (ContainerInterface $container): MyService {
    return new MyService($container->get(LoggerInterface::class));
});

// existing (extend)
$container->factory(
    MyService::class,
    static function (ContainerInterface $container, callable $previous): MyService {
        $myService = $previous($container);
        $myService->setLogger($container->get(LoggerInterface::class));

        return $myService;
    }
);



use Chubbyphp\Container\MinimalContainer;
use Chubbyphp\Container\Parameter;

$container = new MinimalContainer();
$container->factory('key', new Parameter('value'));



use App\Service\MyService;
use Chubbyphp\Container\MinimalContainer;

$container = new MinimalContainer();

$myService = $container->get(MyService::class);



use App\Service\MyService;
use Chubbyphp\Container\MinimalContainer;

$container = new MinimalContainer();
$container->has(MyService::class);



use App\Service\MyService;
use Chubbyphp\Container\Container;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

$container = new Container();
$container->prototypeFactories([
    MyService::class => static function (ContainerInterface $container): MyService {
        return new MyService($container->get(LoggerInterface::class));
    },
]);



use App\Service\MyService;
use Chubbyphp\Container\Container;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

$container = new Container();

// new
$container->prototypeFactory(
    MyService::class,
    static function (ContainerInterface $container): MyService {
        return new MyService($container->get(LoggerInterface::class));
    }
);

// existing (replace)
$container->prototypeFactory(
    MyService::class,
    static function (ContainerInterface $container): MyService {
        return new MyService($container->get(LoggerInterface::class));
    }
);

// existing (extend)
$container->prototypeFactory(
    MyService::class,
    static function (ContainerInterface $container, callable $previous): MyService {
        $myService = $previous($container);
        $myService->setLogger($container->get(LoggerInterface::class));

        return $myService;
    }
);