php-strict / container
常见容器的接口和实现。
v1.0.0
2019-05-06 07:33 UTC
Requires
- php: ^7.1
Requires (Dev)
- codeception/codeception: ^2.5.1
This package is auto-updated.
Last update: 2024-09-16 17:47:26 UTC
README
常见容器的接口和实现。
PSR-11的严格版本,增加了设置/获取引用(而不是值)和获取所有容器条目作为数组的方法。
要求
- PHP >= 7.1
安装
使用Composer安装
composer require php-strict/container
用法
经典用法
use PhpStrict\Container\Container $container = new Container(); $container->set('key1', 1); if ($container->has('key1')) { $var1 = $container->get('key1'); }
设置/获取引用的用法
use PhpStrict\Container\Container $myObject = new stdClass(); $container = new Container(); $container->setByRef('key2', $myObject); $anotherObject =& $container->getRef('key2');
通过构造函数设置容器值
use PhpStrict\Container\Container $container = new Container([ 'key1' => 1, 'key2' => 'value 2', 'key3' => true, ]); if ($container->has('key2')) { $var2 = $container->get('key2'); }
通过回调解包容器
use PhpStrict\Container\Container class MyClass { protected $field1; protected $field2; protected $field3; public function unpacker(array $entries): void { foreach ($entries as $key => $value) { $this->$key = $value; } } } $container = new Container([ 'field1' => 1, 'field2' => 'value 2', 'field3' => true, ]); $myClassObject = new MyClass(); $container->unpackWith([$myClassObject, 'unpacker']);
测试
要执行测试套件,您需要Codeception。
vendor\bin\codecept run