PHP code example of rukavishnikov / psr-container

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

    

rukavishnikov / psr-container example snippets


$config = er = new Container($config); // Default used
//$container = new Container($config, true); // Strict mode used (see below)

if ($container->has(InterfaceOrClass::class)) {
    $instance = $container->get(InterfaceOrClass::class);
}

return [
    ServerRequestInterface::class => ServerRequest::class, // Simple definition
    RouterInterface::class => [ // Full definition
        'class' => Router::class, // Required

        // Constructor injection
        '__construct()' => [
            [
                '/test' => TestController::class,
            ],
        ],
    ],
    TestController::class => [ // Full definition
        'class' => TestController::class, // Required

        // Constructor injection
        '__construct()' => [
            true, // Bool
            123, // Int
            5.0, // Float
            'qwerty', // String
            [1, 2, 3], // Array
            StdClass::class, // Class
            new DateTime(), // Instance
            fn () => function (Container $container) {
                return $container->get(StdClass::class);
            }, // Closure
        ],

        // Property injection
        '$public_a' => true, // Bool
        '$public_b' => 123, // Int
        '$public_c' => 5.0, // Float
        '$public_d' => 'qwerty', // String
        '$public_e' => [1, 2, 3], // Array
        '$public_f' => StdClass::class, // Class
        '$public_g' => new DateTime(), // Instance
        '$public_h' => fn () => function (Container $container) {
            return $container->get(StdClass::class);
        }, // Closure

        // Method injection
        'setA()' => [true], // Bool
        'setB()' => [123], // Int
        'setC()' => [5.0], // Float
        'setD()' => ['qwerty'], // String
        'setE()' => [[1, 2, 3]], // Array
        'setF()' => [StdClass::class], // Class
        'setG()' => [new DateTime()], // Instance
        'setH()' => [
            fn () => function (Container $container) {
                return $container->get(StdClass::class);
            }
        ], // Closure
    ],
    ResponseInterface::class => Response::class, // Simple definition
    EmitterInterface::class => Emitter::class, // Simple definition
];