lwlwilliam / container
Minimal PSR-11 Container
1.0.2
2023-06-27 08:28 UTC
Requires
- psr/container: ^2.0
README
Minimal PSF-11 Container
来源: https://ryangjchandler.co.uk/posts/build-your-own-service-container-in-php-minimal-container
使用
$ composer require psr11_container
<?php
// test.php
include 'vendor/autoload.php';
use Demo\Container;
try {
$container = new Container();
$object = new stdClass();
$object->name = 'hello world';
$container->bind('hello', $object);
print_r($container->get('hello'));
print_r($container->get('hello2'));
} catch (Throwable $e) {
print_r($e);
}
$ php test.php
stdClass Object
(
[name] => hello world
)
Demo\exceptions\ServiceNotFoundException Object
(
[message:protected] => hello2
[string:Exception:private] =>
[code:protected] => 0
[file:protected] => /Users/wu/www/github.com/lwlwilliam/test/php/container/src/Container.php
[line:protected] => 20
[trace:Exception:private] => Array
(
[0] => Array
(
[file] => /Users/wu/www/github.com/lwlwilliam/test/php/container/examples/test.php
[line] => 15
[function] => get
[class] => Demo\Container
[type] => ->
[args] => Array
(
[0] => hello2
)
)
)
[previous:Exception:private] =>
)