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 ...
随机推荐
- SQL SERVER中强制类型转换cast和convert的区别
在SQL SERVER中,cast和convert函数都可用于类型转换,其功能是相同的, 只是语法不同. cast一般更容易使用,convert的优点是可以格式化日期和数值. 代码 select CO ...
- redhat 安装GCC-4.8.3
1.下载gcc-4.8.3安装包 gcc各版本浏览地址:http://ftp.gnu.org/gnu/gcc/ yum install gccyum install gcc-c++ 2.将gcc-4. ...
- MyBatis 返回新增数据的自增id
<insert id="save" parameterType="Vote" useGeneratedKeys="true" keyP ...
- 上海赛区-org.apache.ibatis.type.TypeException: JDBC requires that the JdbcType must be specified for all nullable parameters.
执行此函数的时候报错 解决方法:关闭窗口之后刷新主页面 提示: 传入到xml sql语句中的参数为null时就会出现此错误,需要仔细检查
- js操作select和option
1.动态创建select function createSelect(){ var mySelect = document.createElement_x("select"); m ...
- scrollview上面的图片不上下滑动
self.automaticallyAdjustsScrollViewInsets= NO;
- group_concat()函数总结
group_concat(),手册上说明:该函数返回带有来自一个组的连接的非NULL值的字符串结果.比较抽象,难以理解. 通俗点理解,其实是这样的:group_concat()会计算哪些行属于同一组, ...
- XListview的下拉刷新、上拉加载、用Pull解析XML
做之前需要导入XListview的文件,此是用第三方的xListview实现的,东西没写全.此是在Fragment中实现的 //--------------XListView的布局---------- ...
- LightOJ 1370 Bi-shoe and Phi-shoe(欧拉函数)
题意:题目给出一个欧拉函数值F(X),让我们求>=这个函数值的最小数N,使得F(N) >= F(X); 分析:这个题目有两种做法.第一种,暴力打出欧拉函数表,然后将它调整成有序的,再建立一 ...
- 编译Android各种错误
第一次编译成功,第二次出现Value for 'keystore' is not valid. It must resolve to a single path 打开proj.android\ant. ...