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
|
<?phpclass 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
|
<?phpclass 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,并且获取密码
第一次使用ubuntu的时候 先使用这个命令 sudo passwd root 然后就可以改密码了
- __declspec(dllexport) 和 __declspec(dllimport)的作用
operatordll.h #include <iostream> #ifndef _WIN32 #define DLL_EXPORT#else #ifdef OPERATORDLL_EX ...
- 【最短路】 poj 2387
#include <iostream> #include <stdlib.h> #include <limits.h> #include <string.h& ...
- Openlayer 3 图层列表控件(自定义)
<body><div id="map"></div><div id="layerControl" class=&quo ...
- Viewpager实现网络图片的轮播
//主意:里面用到了第三方的Xutils.jar包和Imageloader.jar包还用到了访问网络,所以要加网络权限 <uses-permission android:name="a ...
- 【转载】GDI 映像方式 之 SetViewportOrgEx 与 SetWindowOrgEx 解析
SetViewportOrgEx 与 SetWindowOrgEx 解析 这两个函数,用来改变视端口和窗口的原点,并都具有改变轴的效果,以致(0,0)不再指左上角. 「视端口」是依据设备坐标(图素)的 ...
- VC与ADO数据库操作
VC与ADO数据库操作 学研部的同志们,大家好! 想开一次学习会,实习时间冲突了,只好把文档发给大家看了.重点推荐李振龙的BMP读图教程! 尤其是大三GIS班的同志,注意了,可能实习用得上的! 一.A ...
- ubuntu中文论坛
http://forum.ubuntu.org.cn/index.php?sid=e40344219c81dbc4b289135a71db4efd
- PAT1008
1008. Elevator (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B The highest building in our city has on ...
- ASP.NET Cache 类
在查找资料的过程中.原来园子里面已经有过分析了.nopCommerce架构分析系列(二)数据Cache. 接下来是一些学习补充. 1.Nop中没有System.Web.Caching.Cache的实现 ...