当前位置:网站首页>一文搞懂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();``};
边栏推荐
- Performance rendering of dSPACE
- Is it safe to open a stock account with the customer's haircut account link? Tell me what you know
- 用CPU方案打破内存墙?学PayPal堆傲腾扩容量,漏查欺诈交易量可降至1/30
- 力扣每日一题-第26天-506.相对名次
- How to choose a securities company when opening an account with a compass? Which is safer
- Demonstration of combination of dream CAD cloud map and GIS
- 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
- Sun Wu plays Warcraft? There is a picture and a truth
- Tencent Open Source Project "Yinglong" est devenu un projet Apache de haut niveau: l'ancien Service à long terme Wechat payment, peut maintenir un million de milliards de niveaux de traitement de flux
- 做自媒体不知道怎样变现?7大变现方法分享
猜你喜欢
What is an SSL certificate and what are the benefits of having an SSL certificate?
Amazon's other side in China
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
x86 CPU,危!最新漏洞引发热议,黑客可远程窃取密钥,英特尔“全部处理器”受影响...
About sizeof() and strlen in array
Why can banana be a random number generator? Because it is the "king of radiation" in the fruit industry
Tencent Open Source Project "Yinglong" est devenu un projet Apache de haut niveau: l'ancien Service à long terme Wechat payment, peut maintenir un million de milliards de niveaux de traitement de flux
Xiaomi routing R4A Gigabit version installation feed+openwrt tutorial (the full script does not need to be hard modified)
ICML 2022 | ByteDance AI Lab proposes a multimodal model: x-vlm, learning multi granularity alignment of vision and language
IE寿终正寝,网友们搞起了真·墓碑……
随机推荐
谷歌创始人布林二婚破裂:被曝1月已提出与华裔妻子离婚,目前身家6314亿美元...
AI越进化越跟人类大脑像!Meta找到了机器的“前额叶皮层”,AI学者和神经科学家都惊了...
How far is the memory computing integrated chip from popularization? Listen to what practitioners say | collision school x post friction intelligence
网上开户股票安全吗?怎么开户呢?
DateTimeFormat放到@RequestBody下是无效的
Rebeco:使用机器学习预测股票崩盘风险
Now, the ear is going into the metauniverse
Gold medal scheme of kaggle patent matching competition post competition summary
股票在网上开户安全吗?在线等答案
現在,耳朵也要進入元宇宙了
DSPACE set zebra crossings and road arrows
PHP uses getid3 to obtain the duration of MP3, MP4, WAV and other media files
Self cultivation and learning encouragement
Two common OEE monitoring methods for equipment utilization
The sign in function completes 03 "IVX low code sign in system production"
Nacos practice record
西电AI专业排名超清北,南大蝉联全国第一 | 2022软科中国大学专业排名
2点睡10点起不算熬夜?除非你每天都能执行
扎克伯格最新VR原型机来了,要让人混淆虚拟与现实的那种
[FPGA] serial port controls temperature acquisition by command