amber / container
简单的 PHP DI 容器。
Requires
- php: >=7.4
- amber/cache: ^1.0@beta
- amber/collection: ^0.6@beta
- amber/validator: ^0.1@dev
- psr/container: ^1.0
Requires (Dev)
- php-coveralls/php-coveralls: ^2.1
- symfony/var-dumper: ^4.2
This package is auto-updated.
Last update: 2024-09-15 22:08:14 UTC
README
容器
简单的 PHP DI 容器。
安装
composer require amber/container
API(草案)
基本用法(PSR-11 兼容)
use Amber\Container\Container; $container = new Container();
bind()
通过一个唯一键将项目绑定到容器的映射中。
bind(string $key, mixed $value) : boolean
参数 string $key 唯一项目键。
参数 mixed $value 项目的值。
返回 bool 成功时为真。如果键已存在,则为假。
通过唯一键将服务绑定到容器。
$container->bind($key, $value);
或者绑定一个类,如下所示。
$container->bind($class);
get()
通过其唯一键从容器的映射中获取一个项目。
get(string $key): mixed
参数 string $key *唯一项目键。
返回 mixed 项目的值。
$container->get($key);
has()
通过其唯一键在容器的映射中检查项目的存在。
has(string $key): bool 参数 string $key *唯一项目键。 返回 bool
$container->has($key);
unbind()
通过其唯一键从容器的映射中解绑一个项目。
unbind(string $key): bool
参数 string $key *唯一项目键。
返回 bool 成功时为真,失败时为假。
$container->unbind($key);
多个操作
bindMultiple()
$container->bindMultiple([$key => $value]);
getMultiple()
$container->getMultiple($keys);
unbindMultiple()
$container->unbindMultiple($keys);
高级用法
make()
通过唯一键将服务和获取容器的映射中的服务。
make(string $class): mixed
参数 string $class 项目的类。
返回 mixed 项目的值。
$container->make($class);
register()
将一个类绑定到容器并返回 ServiceClass。
register(string $class, string $alias = null): ServiceClass
参数 string $class 项目的类。
参数 string $alias 项目的别名。
返回 ServiceClass
$container->register($class);
singleton()
将一个类绑定到容器作为单例并返回 ServiceClassservice。
singleton(string $class, string $alias = null): ServiceClass
参数 string $class 项目的类。
参数 string $alias 项目的别名。
返回 ServiceClass
$container->singleton($class);
getClosureFor()
获取提供的类的方法的闭包。
getClosureFor(string $class, string $method, array $binds = []): Closure
参数 string $class 要实例化的类。
参数 string $method 要调用的类方法。
参数 array $binds 服务的参数。
返回 Closure
$container->getClosureFor($class, $method);
更多即将推出...