PHP code example of phpdevcommunity / psr11-dependency-injection

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

    

phpdevcommunity / psr11-dependency-injection example snippets




use Psr\Container\ContainerInterface;

return [
    'database.host' => '127.0.0.1',
    'database.port' => null,
    'database.name' => 'my_database',
    'database.user' => 'root',
    'database.password' => null,
    'google.key' => 'YQ4FcwaXD165Xm72lx53qzzNzkz7AUUN',
    PDO::class => static function (ContainerInterface $container) {
        return new PDO(
            sprintf('mysql:host=%s;dbname=%s;', $container->get('database.host'), $container->get('database.name')),
            $container->get('database.user'),
            $container->get('database.password'),
            [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
        );
    },
];



use PhpDevCommunity\DependencyInjection\Container;

$services = pdo = $container->get(PDO::class);
var_dump($pdo); // Outputs: object(PDO)[18]

// Retrieve a simple configuration value
$googleKey = $container->get('google.key');
var_dump($googleKey); // Outputs: YQ4FcwaXD165Xm72lx53qzzNzkz7AUUN



use PhpDevCommunity\DependencyInjection\Container;
use PhpDevCommunity\DependencyInjection\ReflectionResolver;
use App\Service\MyService;

$services = ice::class);
var_dump($myService); // Outputs: object(MyService)