php dependency innjection
You’ve probably heard of the acronym SOLID by now, which is an object oriented programming paradigm consisting of five basic (but interrelated principles) of object oriented development.
And you’ve probably heard once or twice that the D in SOLID stands for Dependency Injection. Actually, if you’re lucky you’ve also heard what it really stands for, which is the Dependency Inversion Principle. But, in PHP, this is often conflated with dependency injection.
I’m here to tell you today that dependency injection is only one piece of the overall puzzle in understanding this crucial principle.
The Dependency Inversion Principle states that:
Do not depend upon concretions; depend upon abstractions instead.
A. High-level modules should not depend on low-level modules. Both should depend on abstractions.
B. Abstractions should not depend upon details. Details should depend upon abstractions.
Dependency injection is often cited as one way of complying with this principle, by injecting dependencies into lower level modules rather than depending on concrete instances that are created in the lower levels. But this is only one part of the dependency inversion principle.
This particular principle also hinges on the concept of “dependency on abstractions.” When type hinting in PHP it is possible to type hint on a concrete instance of a class (e.g. a type that can be instantiated and used). However, this makes an object just as dependent on a concretion as it would be if it was instantiating the object directly; while it becomes easier to write tests with this mechanism, we are still no better off in terms of depending on an abstraction. For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?php class myClass { public function __construct() { $this ->myDatabase = new Database(); } } class myOtherClass { public function __construct(Database $db ) { $this ->myDatabase = $db ; } } |
Other than the improvement in testability of myOtherClass over myClass, there is no difference in the dependencies of the two classes. They are both dependent upon the concrete class Database.
Instead, it is better for us to depend upon a defined interface or abstract class. By type hinting on the interface, rather than the concrete implementation of the interface, we are depending upon the abstraction provided by the interface and honoring the dependency inversion principle completely. Additionally, because we are using an interface, the second part of the dependency inversion principle is honored as well (details depending upon abstractions).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?php class myClass { public function __construct() { $this ->myDatabase = new Database(); } } class myOtherClass { public function __construct(Database_Interface $db ) { $this ->myDatabase = $db ; } } |
In the second example, there is a huge difference between the two classes: the myOtherClass object is now dependent upon an abstraction of Database through Database_Interface; that interface can be implemented in any way that the developer needs or wants and will still work with the myOtherClass code. This honors the dependency inversion principle through dependence on abstractions as well as through injection of dependencies.
http://www.brandonsavage.net/the-d-doesnt-stand-for-dependency-injection/
There is a difference, but it’s subtle.
The Liskov substitution principle says that objects should be replaceable with instances of their subtypes. By type hinting an abstraction we are making that true. If we were talking about type hinting, this would be an article on LSP.
However, this relates to dependency inversion because we are focusing on the relationship of an object to its dependencies, which should be abstractions, not concretions. The only way in PHP to depend upon abstractions is to type hint an abstraction.
The difference is subtle, but there. And LSP and DIP are very closely related. In fact, all of the SOLID principles relate to one another in that it’s nearly impossible to apply one without applying all of them
php dependency innjection的更多相关文章
- 启用SQLite的Data Provider 运行WECOMPANYSITE时遇到ERROR CREATING CONTEXT 'SPRING.ROOT': ERROR THROWN BY A DEPENDENCY OF OBJECT 'SYSTEM.DATA.SQLITE'
从网上下载的源码WeCompanySite,运行时报错 Error creating context 'spring.root': Error thrown by a dependency of ob ...
- podfile The dependency `` is not used in any concrete target
内容提要: podfile升级之后到最新版本,pod里的内容必须明确指出所用第三方库的target,否则会出现The dependency `` is not used in any concrete ...
- Ninject学习(一) - Dependency Injection By Hand
大体上是把官网上的翻译下而已. http://www.ninject.90iogjkdcrorg/wiki.html Dependency Injection By Hand So what's Ni ...
- [IOS]使用了cocoapods 抱错Pods was rejected as an implicit dependency for ‘libPods.a’ because its architectures ......
Pods was rejected as an implicit dependency for ‘libPods.a’ because its architectures ‘i386’ didn’t ...
- 在.NET Core中遭遇循环依赖问题"A circular dependency was detected"
今天在将一个项目迁移至ASP.NET Core的过程中遭遇一个循环依赖问题,错误信息如下: A circular dependency was detected for the service of ...
- <dependency>
<dependency> <groupId>org.hibernate</groupId> ...
- AngularJS之Dependency Injection(五)
前言 这一节我们来讲讲AngularJS中的依赖注入,在实际开发中Angular提供了具体的方法供我们去调用,但是一旦业务不能满足要求或者出现麻烦或者错误时导致无从下手,所以基于此我们有必要深入一点去 ...
- Dependency management
Play’s dependency management system allows you to express your application’s external dependencies i ...
- Gradle's dependency cache may be corrupt解决方法
问题描述: Error:Unable to find method 'com.google.common.cache.CacheBuilder.build(Lcom/google/common/cac ...
随机推荐
- 禁止root远程登录 sshd问题:A protocol error occurred. Change of username or service not allowed
在研究Linux安全的时候遇到一个问题,原本打算修改linux直接远程root登陆,修改为sshd的配置文件后 Nano /etc/ssh/sshd_config 把#PermitRootLogin ...
- zzuli 1919 数列划分
题面: Description 晴天想把一个包含n个整数的序列a分成连续的若干段,且和最大的一段的值最小,但他有强迫症,分的段数不能超过m段,然后他就不会分了...他想问你这个分出来的和最大的一段的和 ...
- less分页阅读
less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大.less 的用法比起 more 更加的有弹性.在 more 的时候,我们并没有办法向前面翻 ...
- Swift 使用Extension 场景 浅析
别人一看到我的 Swift 代码,立刻就会问我为什么如此频繁的使用 extension.这是前几天在我写的另一篇文章中收到的评论: 我大量使用 extension 的主要目的是为了提高代码可读性.以下 ...
- IOS 使用动态库(dylib)和动态加载framework
在iphone上使用动态库的多为dylib文件,这些文件使用标准的dlopen方式来使用是可以的.那相同的在使用framework文件也可以当做动态库的方式来动态加载,这样就可以比较自由的使用appl ...
- makefile的编写规则
2. 编写makefile 示例: test:main.o func.o gcc -o test main.o func.o func.o:func.c gcc -c func.c mai ...
- 转:HTML错误编号大全
HTML错误编号大全 状态行包含HTTP版本.状态代码.与状态代码对应的简短说明信息.在大多数情况下,除了Content-Type之外的所有应答头都是可选的.但Content-Type是必需的,它描述 ...
- Linux 部署 Tomcat和JDK
一:安装jdk下载将jdk加压后放到/usr/local目录下: [root@master ~]#chmod 755 jdk-6u5-linux-x64.bin [root@master ~]# ./ ...
- redmine配置邮件
作为一个项目管理平台,必须能够通知项目成员有关项目和任务的各种状态变化.这也是一种典型的观察者模式.开发人员可以跟踪某个任务,当任务状态.备注.文档.优先级等变化时开发人员就可以得到通知. 对于新配置 ...
- jvisualvm
f the fonts used by VisualVM are hard to read, switching the LaF might help. Try for example 'visu ...