mohamed-amine/ioc-container
Composer 安装命令:
composer require mohamed-amine/ioc-container
包简介
Inversion of control container
README 文档
README
How to install:
Via composer
composer require mohamed-amine/ioc-container
What can it do:
- create instances and resolve their dependencies automatically.
- resolve dependencies from types( like Interfaces) if they are registered.
- create instances from registred services locators.
- bind existing instances into the container (can be used for service providers).
- call instances from the container anywhere in your application.
How to use it:
Create the contianer:
in order to use all the features we need to create a container first
IOC\IOC::createContainer();
Create an instance in the container:
- to create an instance and resolve it's dependencies automatically:
IOC\IOC::container()->build('namespace\MyClass');
This will create the MyClass object and create all of it's dependencies recursively for example:
// instead of doing this new MyClass(new ClassA(), new ClassB()); // you can just do this and it will create ClassA & ClassB and pass them automatically IOC\IOC::container()->build('namespace\MyClass');
In order for this to work you need to use type hinting in MyClass constructor :
... public function __constructor(ClassA $classa, ClassB $classb) {...} ...
- if the instance require arguments we should pass them in an array as a second argument of 'build()' (note that automatic dependencies resolving can't be used in this case):
IOC\IOC::container()->build('namespace\MyClass', $argument1, $argument2);
Bind instances into the container:
To bind an existing instance into the container
IOC\IOC::bind('MyClass',function() { return new namespace\MyClass(); });
Now we will be able to access our instance anywhere in the application like this:
IOC\IOC::container()->MyClass; // or IOC\IOC::container()->get('MyClass');
Build instances from services locators (short names):
We can define service locators using the regiter method
IOC\IOC::container()->register('MyClass', namespace\MyClass::class);
Then we can build an instance using this service locator
IOC\IOC::container()->build('MyClass');
Registering Types:
Types are registered to help the container to resolve classes dependencies, for example if a class A have a dependency of Type SomeInterface which is an interface, we will have to register this type first in order to be able to resolve it:
IOC\IOC::container()->registerType('SomeInterface', Namespace\SomeClass::class);
Calling instances from the container:
we can call instances from the container using 2 methods
IOC\IOC::container()->MyInstance; // Or IOC\IOC::container()->get('MyInstance');
mohamed-amine/ioc-container 适用场景与选型建议
mohamed-amine/ioc-container 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 432 次下载、GitHub Stars 达 4, 最近一次更新时间为 2018 年 01 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 mohamed-amine/ioc-container 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mohamed-amine/ioc-container 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 432
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 15
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-01-30