PHP code example of substancephp / container

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

    

substancephp / container example snippets


use Laminas\Diactoros\ServerRequestFactory;
use Psr\Http\Message\ServerRequestInterface;
use SubstancePHP\Container\Container;
use SubstancePHP\Container\Inject;

// Initialize a `Container` instance from an array. The array's keys are strings
// (typically, but not necessarily, class, enum or interface names), and the array
// values are callbacks telling the container how to construct each given dependency.

$container = Container::from([

    // Example of simple "manual" definition:
    Foo::class => function (): Foo {
        return new Foo('example', 'constructor', 'parameters');
    },

    // Dependencies can also be literal values; and we can use arrow functions for
    // brevity:
    'ttl-seconds' => fn () => 30,

    // Example of manual definition by passing the container to the callback.
    // This can be used to get other dependencies ner` instance per HTTP
// request, if your application's architecture tuff
});

// Similarly, if a class is autowired using Container::autowire(...), you can
// use the `Inject` attribute in its constructor, to specify dependencies for
// parameters that cannot be inferred automatically:

public function __construct(
    Bar $bar,
    #[Inject('thing.xyz')] int $someParam,
) {
}