当前位置:网站首页>一文搞懂php中的(DI)依赖注入
一文搞懂php中的(DI)依赖注入
2022-06-25 03:40:00 【InfoQ】
前言
一、什么是依赖注入 (DI)
二、依赖注入出现的原因
class` `container``{`` ``private` `$adapter``;` ` ``public` `function` `__construct()`` ``{`` ``$this``->adapter = ``new` `adapter();`` ``}`` ``}
三、简单的依赖注入
class` `container``{`` ``private` `$adapter``;` ` ``public` `function` `__construct(adapter ``$adapter``)`` ``{`` ``$this``->adapter = ``$adapter``;`` ``}``}
四、高阶的依赖注入
class` `container``{`` ``public` `$instance` `= [];`` ` ` ``public` `function` `__set(``$name``, ``$value``)`` ``{`` ``$this``->instance[``$name``] = ``$value``;`` ``}``}`` ` `$container` `= ``new` `container();` `$container``->adapter = ``new` `adapter();
五、依赖注入的应用
class` `container``{`` ``public` `$instance` `= [];`` ` ` ``public` `function` `__set(``$name``, ``$value``)`` ``{`` ``$this``->instance[``$name``] = ``$value``;`` ``}``}` `class` `adapter``{`` ``public` `$name` `= ``'我是调度器'``;``}` `$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``); ``//我是调度器
六、高阶优化
$container` `= ``new` `container();``$container``->adapter = ``new` `adapter();`` ` ` ` `//高阶优化``$container` `= ``new` `container();``$container``->adapter = ``function` `() {`` ``return` `new` `adapter();``};
边栏推荐
- 做自媒体不知道怎样变现?7大变现方法分享
- The more AI evolves, the more it resembles the human brain! Meta found the "prefrontal cortex" of the machine. AI scholars and neuroscientists were surprised
- New solution of 202112-2 sequence query
- Program. Launch (xxx) open file
- The programmer reality show is coming again! Hulan, as the host, carried the lamp to fill the knowledge. The SSS boss had a bachelor's degree in pharmacy. Zhu Jun and Zhang Min from Tsinghua joined th
- ASP.NET会议室预约小程序源码 预约小程序源码
- 中国天眼发现地外文明可疑信号,马斯克称星舰7月开始轨道试飞,网信办:APP不得强制要求用户同意处理个人信息,今日更多大新闻在此...
- [proteus simulation] Arduino uno+ relay controls lighting equipment
- Single case of hungry and lazy mode
- Easynvr fails to use onvif to detect the device. What is the reason why "no data" is displayed?
猜你喜欢

現在,耳朵也要進入元宇宙了

MySQL installation tutorial

MySql安装教程

What is an SSL certificate and what are the benefits of having an SSL certificate?

Two way combination of business and technology to build a bank data security management system

用CPU方案打破内存墙?学PayPal堆傲腾扩容量,漏查欺诈交易量可降至1/30

A new generation of cascadable Ethernet Remote i/o data acquisition module

Zuckerberg's latest VR prototype is coming. It is necessary to confuse virtual reality with reality

AI自己写代码让智能体进化!OpenAI的大模型有“人类思想”那味了

Performance rendering of dSPACE
随机推荐
站在风暴中心:如何给飞奔中的腾讯更换引擎
Is it safe to open an account online? How to open an account?
Now, the ear is going into the metauniverse
nacos实践记录
西电AI专业排名超清北,南大蝉联全国第一 | 2022软科中国大学专业排名
什么是SSL证书,拥有一个SSL证书有什么好处?
在线股票开户安全吗?
大咖说*计算讲谈社|如何提出关键问题?
How far is the memory computing integrated chip from popularization? Listen to what practitioners say | collision school x post friction intelligence
股票开户,在手机上开户安全吗?
同花顺证券开户安全吗
ASP.NET会议室预约小程序源码 预约小程序源码
Two common OEE monitoring methods for equipment utilization
A new generation of cascadable Ethernet Remote i/o data acquisition module
Administrator如何禁止另一个人踢掉自己?
[proteus simulation] Arduino uno+ relay controls lighting equipment
Why can banana be a random number generator? Because it is the "king of radiation" in the fruit industry
EasyNVR使用Onvif探测设备失败,显示“无数据”是什么原因?
@PostConstruct
指南针在上面开户安全吗?靠谱吗?