devcoder-xyz / php-dependency-injection
A simple PSR-11 PHP Dependency Injection
1.0.0
2021-01-29 13:03 UTC
Requires
- php: >=7.3
- psr/container: ^1.0
Requires (Dev)
- phpunit/phpunit: ^8.1
README
PHP version required 7.3
How to use ?
// services.php <?php use Psr\Container\ContainerInterface; return [ 'database.host' => '127.0.0.1', 'database.port' => null, 'database.name' => '', '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] ); }, ];
<?php use DevCoder\DependencyInjection\Container; $services = require 'services.php'; $container = new Container($services); var_dump($container->get(PDO:class)); // object(PDO)[18] var_dump($container->get('google.key')); // YQ4FcwaXD165Xm72lx53qzzNzkz7AUUN
Ideal for small project Simple and easy! https://github.com/devcoder-xyz/php-dependency-injection
How can we improve this object?
- add autowire option to Auto resolution of constructor parameters