Home ยป What is Dependency Injection?
dependency-injection
PHP

What is Dependency Injection?

By my opinion one of the biggest problems in programming are dependencies. If you want to have a good well written application you should avoid dependencies between your modules/classes. There is a design pattern which could help and it’s called Dependency Injection (DI). It allows you to inject objects into a class, instead of creating them there.

Dependency injection is a procedure where one object supplies the dependencies of another object. Dependency Injection is a software design approach that allows avoiding hard-coding dependencies and makes it possible to change the dependencies both at runtime and compile time.

In simple terms, Dependency Injection is a design pattern that helps avoid hard-coded dependencies for some piece of code or software.

The dependencies can be changed at run time as well as compile time. We can use Dependency Injection to write modular, testable and maintainable code:

  • Modular: The Dependency Injection helps create completely self-sufficient classes or modules
  • Testable: It helps write testable code easily eg unit tests for example
  • Maintainable: Since each class becomes modular, it becomes easier to manage it

Types of Dependency Injection

There are mainly three types of Dependency Injection:

  • Constructor Injection: In this type of injection, the injector supplies dependency through the client class constructor.
  • Setter Injection / Property Injection: In this type of injection, the injector method injects the dependency to the setter method exposed by the client.
  • Interface Injection: In this type of injection, the injector uses Interface to provide dependency to the client class. The clients must implement an interface that will expose a setter method which accepts the dependency.

For example let’s take the following example:

class Person {	
	private $skills;
	
	public function __construct() {
		$this->skills = array("PHP", "HTML", "CSS", "JavaScript");	
	}	
	public function totalSkills() {
		return count($this->skills);	
	}
}
$p = new Person();echo $p->totalSkills();

There are three big problems with the class above.

  • When we want to add some new skill we should edit the class, which is not good. Every class should be written as a black box. I.e. you should be able to fully operate with the class only by using its public properties and variables.
  • What if we want to set different skills for the different instances of class Person. It is currently not possible, because the private property $skills is created inside the constructor of the class.
  • What if we have something more then an array. For example another object and that object has its own needs. Then we should send the needed things to Person class so we can initialize our variable.

The solution of the problem could be something like:

class Person {
	private $skills;
	
	public function __construct($skills) {
		$this->skills = $skills;
	}
	public function totalSkills() {
		return count($this->skills);
	}
}
$mySkills = array("PHP", "HTML", "CSS", "JavaScript");
$p = new Person($mySkills);
echo $p->totalSkills();

I.e. passing the skills from outside the class. We could say that we injected an object in class Person. The example is really simple, but it perfectly illustrates how helpful could be this approach.In the example above we used injection to make our class looks better.

1 Comment

Click here to post a comment

+ 39 = 44