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

时间: 2024-10-22 15:33:58

php dependency innjection的相关文章

Sentiment Analysis(1)-Dependency Tree-based Sentiment Classification using CRFs with Hidden Variables

The content is from this paper: Dependency Tree-based Sentiment Classification using CRFs with Hidden Variables, by Tetsuji Nakagawa. A typical approach for sentiment classification is to use supervised machine learning algorithms with bag-of-words a

This dependency was not found: * !!vue-style-loader!css-loader?……解决方案

Webstorm2017.1.4 new里找不到vue文件的处理方法 这一篇中说到加的模板是这样的. 但是当你新建一个vue项目时,需要重新安装stylus,否则报错: This dependency was not found: * !!vue-style-loader!css-loader?{"minimize":false,"sourceMap":false}!../../node_modules/vue-loader/lib/style-compiler/i

Maven类包冲突终极三大解决技巧 mvn dependency:tree

Maven对于新手来说是<步步惊心>,因为它包罗万象,博大精深,因为当你初来乍到时,你就像一个进入森林的陌生访客一样迷茫. Maven对于老手来说是<真爱配方>,因为它无所不能,利如刀锋,使用Maven做开发,如饮美酒如悦美人. Maven对于新手来说,最痛苦的一件事莫过于包之间的冲突,由于Maven的依赖传递性,当你引入一个依赖类时,其身后的依赖类也一起如过江之鲫纷至沓来了. 举例 A依赖于B及C,而B又依赖于X.Y,而C依赖于X.M,则A除引B及C的依赖包下,还会引入X,Y,M

7) mvn dependency:tree

http://maven.apache.org/plugins/maven-dependency-plugin/tree-mojo.html mvn dependency:tree 查看 <dependency> <groupId>groupId_out</groupId> <artifactId>artifactId_out</artifactId> <version>version_out</version> <

Eclipse导入library的时候报:Found 2 versions of android-support-v4.jar in the dependency list

错误类型: [2014-04-28 11:56:03 - 新闻] Found 2 versions of android-support-v4.jar in the dependency list, [2014-04-28 11:56:03 - 新闻] but not all the versions are identical (check is based on SHA-1 only at this time). [2014-04-28 11:56:03 - 新闻] All versions

maven可选依赖(Optional Dependencies)和依赖排除(Dependency Exclusions)

我们知道,maven的依赖关系是有传递性的.如:A-->B,B-->C.但有时候,项目A可能不是必需依赖C,因此需要在项目A中排除对A的依赖.在maven的依赖管理中,有两种方式可以对依赖关系进行,分别是可选依赖(Optional Dependencies)以及依赖排除(Dependency Exclusions). 一.可选依赖 当一个项目A依赖另一个项目B时,项目A可能很少一部分功能用到了项目B,此时就可以在A中配置对B的可选依赖.举例来说,一个类似hibernate的项目,它支持对mys

记一下Grapht Dependency Injector.jar的下载地址

Grapht Dependency Injector-0.2.1.jar http://www.dsjkf.cn/jar/11556.html Grapht Dependency Injector-0.3.1.jar http://www.dsjkf.cn/jar/11555.html Grapht Dependency Injector-0.5.0.jar http://www.dsjkf.cn/jar/11553.html

Spring mvc 4系列教程(二)——依赖管理(Dependency Management)和命名规范(Naming Conventions)

依赖管理(Dependency Management)和命名规范(Naming Conventions) 依赖管理和依赖注入(dependency injection)是有区别的.为了将Spring的优秀特性(如依赖注入)带到你的应用中,需要在编译时或运行时部署所需要的库(jar包).这些依赖不是虚拟的构件,而是文件系统上的物理资源.依赖管理的过程涉及到定位这些资源.存储资源.加入classpath.依赖可以是直接的(例如Spring运行时),也可以是间接的(例如commons-dbcp).间接

WPF基础学习笔记(一)Dependency Object 和 Dependency Property

.依赖属性是WPF个人觉得对精彩和最有特色的部分.所以特地先拿出来. 首先要实现Dependency Property 则必须要继承Dependency Object.如果看下WPF的基础控件其实都间接或者直接继承Dependency Object,这点规律性我在后面再做规律陈述. 首先Dependency Object 我搬下书的描述 Only the thread that the Dispatcher was created on may access the DispatcherObje