当前位置:网站首页>Understand (DI) dependency injection in PHP
Understand (DI) dependency injection in PHP
2022-06-25 03:51:00 【InfoQ】
Preface
One 、 What is dependency injection (DI)
Two 、 Why dependency injection occurs
class` `container``{`` ``private` `$adapter``;` ` ``public` `function` `__construct()`` ``{`` ``$this``->adapter = ``new` `adapter();`` ``}`` ``}
3、 ... and 、 Simple dependency injection
class` `container``{`` ``private` `$adapter``;` ` ``public` `function` `__construct(adapter ``$adapter``)`` ``{`` ``$this``->adapter = ``$adapter``;`` ``}``}
Four 、 Higher order dependency injection
class` `container``{`` ``public` `$instance` `= [];`` ` ` ``public` `function` `__set(``$name``, ``$value``)`` ``{`` ``$this``->instance[``$name``] = ``$value``;`` ``}``}`` ` `$container` `= ``new` `container();` `$container``->adapter = ``new` `adapter();
5、 ... and 、 Application of dependency injection
class` `container``{`` ``public` `$instance` `= [];`` ` ` ``public` `function` `__set(``$name``, ``$value``)`` ``{`` ``$this``->instance[``$name``] = ``$value``;`` ``}``}` `class` `adapter``{`` ``public` `$name` `= ``' I'm the scheduler '``;``}` `$container` `= ``new` `container();` `$container``->adapter = ``new` `adapter();` ` ` `class` `autofelix``{`` ``private` `$container``;`` ` ` ``public` `function` `__construct(container ``$container``)`` ``{`` ``$this``->container = ``$container``;`` ``}`` ` ` ``public` `function` `who(``$class``)`` ``{`` ``return` `$this``->container->instance[``$class``]->name;`` ``}``}`` ` `$autofelix` `= ``new` `autofelix(``$container``);`` ` `$who` `= ``$autofelix``->who(``'adapter'``);`` ` `var_dump(``$who``); ``// I'm the scheduler
6、 ... and 、 Higher order optimization
$container` `= ``new` `container();``$container``->adapter = ``new` `adapter();`` ` ` ` `// Higher order optimization ``$container` `= ``new` `container();``$container``->adapter = ``function` `() {`` ``return` `new` `adapter();``};
边栏推荐
- 腾讯开源项目「应龙」成Apache顶级项目:前身长期服务微信支付,能hold住百万亿级数据流处理...
- Rebeco: using machine learning to predict stock crash risk
- 马斯克:推特要学习微信,让10亿人「活在上面」成为超级APP
- Administrator如何禁止另一个人踢掉自己?
- Easynvr fails to use onvif to detect the device. What is the reason why "no data" is displayed?
- Collaboration + Security + storage, cloud box helps Shenzhen edetai restructure its data center
- Wuenda, the new course of machine learning is coming again! Free auditing, Xiaobai friendly
- 发布功能完成02《ivx低代码签到系统制作》
- 谷歌创始人布林二婚破裂:被曝1月已提出与华裔妻子离婚,目前身家6314亿美元...
- AI自己写代码让智能体进化!OpenAI的大模型有“人类思想”那味了
猜你喜欢
Easynvr fails to use onvif to detect the device. What is the reason why "no data" is displayed?
可能是拿反了的原因
后台页制作01《ivx低代码签到系统制作》
Tensorflow, danger! Google itself is the one who abandoned it
DevEco Studio 3.0编辑器配置技巧篇
Maybe it's the wrong reason
程序猿职业发展9项必备软技能
Insurance app aging service evaluation analysis 2022 issue 06
AI writes its own code to let agents evolve! The big model of openai has the flavor of "human thought"
TC object structure and abbreviation
随机推荐
When people look at the industrial Internet from the Internet like thinking and perspective, they have actually fallen into a dead end
Is it safe to open a stock account on Huatai Securities?
Rebeco:使用机器学习预测股票崩盘风险
[proteus simulation] Arduino uno+ nixie tube display 4X4 keyboard matrix keys
如何使用IDE自动签名调试鸿蒙应用
太极图形60行代码实现经典论文,0.7秒搞定泊松盘采样,比Numpy实现快100倍
DateTimeFormat放到@RequestBody下是无效的
跨境电商新手如何防止店铺关联?用什么工具好?
A new generation of cascadable Ethernet Remote i/o data acquisition module
BGP biplane architecture
Mobile mall project operation
IE寿终正寝,网友们搞起了真·墓碑……
CVPR大会现场纪念孙剑博士,最佳学生论文授予同济阿里,李飞飞获黄煦涛纪念奖...
香蕉为什么能做随机数生成器?因为,它是水果界的“辐射之王”
Is it reliable for CITIC Securities to open a mobile account? Is it safe?
Tensorflow, danger! Google itself is the one who abandoned it
Sleep more, you can lose weight. According to the latest research from the University of Chicago, sleeping more than 1 hour a day is equivalent to eating less than one fried chicken leg
How to raise key issues in the big talk club?
扎克伯格最新VR原型机来了,要让人混淆虚拟与现实的那种
Rebeco: using machine learning to predict stock crash risk