PHP code example of kbondurant / self-provider-container

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

    

kbondurant / self-provider-container example snippets


use Kbondurant\SelfProviderContainer\SelfProviderContainer;
use League\Container\Container;
use League\Container\ReflectionContainer;

$container = new Container();

$container->delegate(new SelfProviderContainer());
$container->delegate(new ReflectionContainer());

class LeagueRouter implements ServiceProvider
{
    public function __construct(
        private Router $router,
    ) {
    }

    /**
     * @param \League\Container\DefinitionContainerInterface $container
     * @return void
     */
    public static function register(mixed $container): void
    {
        $container->add(Router::class, fn () => new Router())
            ->setShared(true);
    }
}

use Acme\Container\MyOwnContainer;
use Kbondurant\SelfProviderContainer\SelfProviderContainer;
use League\Container\Container;
use League\Container\ReflectionContainer;

$container = new Container();

$container->delegate(new SelfProviderContainer(new MyOwnContainer()));
$container->delegate(new ReflectionContainer());

class LeagueRouter implements ServiceProvider
{
    public function __construct(
        private Router $router,
    ) {
    }

    /**
     * @param \Acme\Container\MyOwnContainer $container
     * @return void
     */
    public static function register(mixed $container): void
    {
        $container->singleton(Router::class, fn () => new Router());
    }
}