Dependency Injection in PHP
What is Dependency Injection?
Dependency Injection is a software design pattern that helps you to avoid hard-coding dependencies and makes it possible to change the dependencies at runtime and compile time.
The following example with 2 classes named Employee, Company will help you to understand what is dependency injection. Let us consider the following example. Normally we are following this type of coding.
<?php
class Employee {
private $firstName;
private $lastName;
public function __construct($firstName, $lastName) {
$this->firstName = $firstName;
$this->lastName = $lastName;
}
public function getFirstName() {
return $this->firstName;
}
public function getLastName() {
return $this->lastName;
}
}
Employee.php
<?php
class Company {
private $employee;
private $companyName;
public function __construct($companyName, $employeeFirstName, $employeeLastName) {
$this->employee = new Employee($employeeFirstName, $employeeLastName);
$this->$companyName = $companyName;
}
public function getEmployee() {
return $this->employee;
}
public function getCompanyName() {
return $this->companyName;
}
}
Company.php
At first glance, we cannot see any issues. But actually, there are problems with this code.
· The employee’s information passed to the company constructor has nothing to do inside the company’s scope. The name of an employee should be inside the employee class because it has nothing to do with the company itself.
· The employee class is tightly coupled with the Company class. If we add a new parameter to employee’s constructor, we then have to modify every class where we create an Employee object.
Dependency injection solves these issues by inserting the dependencies through the dependent class' constructor ("Constructor Injection"). which might look like this:
<?php
class Employee {
private $firstName;
Private $lastName;
public function __construct($firstName, $lastName) {
$this->firstName = $firstName;
$this->lastName = $lastName;
}
public function getFirstName() {
return $this->firstName;
}
public function getLastName() {
return $this->lastName;
}
}
Employee.php
<?php
class Company {
private $employee;
private $companyName;
public function __construct($companyName, Employee $employee) {
$this->employee = $employee;
$this->$companyName = $companyName;
}
public function getEmployee() {
return $this->employee;
}
public function getCompanyName() {
return $this->companyName;
}
}
Company.php
Types of Injections
Two types of injections are there.
- Constructor Injection
- Setter Injection
Constructor Injection
Advantages
· Using constructor injection we can guarantee that the required dependencies are present.
· Constructor injection makes it sure that the dependency can’t be changed or altered during the object lifetime.
Drawbacks
· In the case of optional dependencies, constructor injection is not suitable.
· It is difficult to extend and override the constructor for class inheritance.
Setter Injection
Setter injection is a commonly used type of dependency injection. An example is given below
<?php
class Company {
private $employee;
private $companyName;
public function __construct($companyName) {
$this->companyName = $companyName;
}
public function getEmployee() {
return $this->employee;
}
public function getcompanyName() {
return $this->companyName;
}
public function setEmployeeName(Employee $employee) {
$this->employee = $employee;
}
}
Advantages of Setter Injection
· It can be used for optional dependencies and the class can be created with default values.
· We can easily add new dependencies as adding a new setter method and it will not break any existing code.
Conclusion
If we are working on large, complex, and long-running project dependency injection will help you to write more maintainable, testable, and modular code. For any Laravel Development Service contact us.
Comments
Post a Comment