bonzer / ioc-container
控制反转容器 - 促进面向接口设计
v0.0.2
2023-09-10 16:09 UTC
Requires
- php: >=5.4.0
- bonzer/exceptions: v0.0.1
This package is not auto-updated.
Last update: 2024-09-23 12:14:45 UTC
README
促进面向接口设计
入门指南
先决条件
- PHP >= 5.4
- "bonzer/exceptions" : "dev-master"
安装
可以使用composer安装。运行
composer require bonzer/ioc-container
用法
use Bonzer\IOC_Container\facades\Container as App_Container;
绑定到容器
有两种方法可用于绑定您的类与容器。
bind
或 singleton
用于将类与容器绑定。
用法示例
App_Container::bind('My_Class', 'My\Namespace\My_Class');
App_Container::singleton('My_Class', 'My\Namespace\My_Class');
绑定接口
您还可以将接口与容器绑定,例如
App_Container::bind('My\Namespace\My_Interface', 'My\Namespace\My_Class');
每当您在其他类中使用此接口,并且该类已通过 IOC_Container
绑定和/或实例化时,接口将自动解析。
实例化
要实例化,只需使用绑定的键调用 App_Container::make
App_Container::make('My_Class');
- 注意:如果您使用
singleton
绑定,则App_Container::make
将每次返回相同的实例。
直接实例化
也可以直接实例化,无需绑定。例如。
App_Container::make('My\Namespace\My_Class');
对于不绑定而进行单例实例化,请使用
App_Container::make_singleton('My\Namespace\My_Class');
- 注意:如果您的类有私有构造函数,则
IOC_Container
会检查以下两个静态方法之一的可用性:init
或get_instance
支持
如果您遇到问题,请告诉我。
您可以通过 ralhan.paras@gmail.com 联系我
许可证
该项目受MIT许可证许可。