php-dependency-injection-825x350Dependency injection has been a frequent topic for discussion among corporate PHP developers and many feared that they’ll overspend the time on building the application architecture without doing any real work. With the help of this article PHP developers will understand to consider taking the advantage of dependency injection while building large scale projects.

What is Dependency Injection?

Before digging any further it is important to understand what dependency injection really means. It is one of the simple design patterns that can be used. Let’s take some of the most simple web examples to demonstrate the usage of Dependency injection.

 

Let us assume that we are currently working on a “questions and answers” website. SO you are more likely to create a class by the name of “Questions”, which would contain member of type “Author”.

In the old days, programmers would have directly created the “Author” object directly in the “questions” constructor like this following code:

 

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

class Author {

   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;

   }

}

class Question {

   private $author;

   private $question;

   public function __construct($question, $authorFirstName, $authorLastName) {

       $this->author = new Author($authorFirstName, $authorLastName);

       $this->question = $question;

   }

   public function getAuthor() {

       return $this->author;

   }

   public function getQuestion() {

       return $this->question;

   }

}

Many Coders would have called this a good code but there are many problems with it:

 

  • The author’s information passed directly to the “Questions” constructor has nothing to do inside the question’s scope. The name of the author should be inside the “Author” class as it has nothing to with the question itself.
  • The “Author” class is tightly knitted with the “Questions” class. If we want to add a new parameter to “Author’s” constructor, we then have to modify every class in order to create an “Author”  object- which is obviously a very tedious and long process, especially in large applications. It becomes very error prone in dealing with large projects.
  • If you want to isolate and test “Questions” class then the unwanted behaviour of having to test the “Author” class as well happens.

 

Dependency injection deals with these kind of errors and alleviates with them with minor fixes in the code by creating a dependent class constructor(“constructor injection”). The new code looks like this:

 

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

class Author {

   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;

   }

}

class Question {

   private $author;

   private $question;

   public function __construct($question, Author $author) {

       $this->author = $author;

       $this->question = $question;

   }

   public function getAuthor() {

       return $this->author;

   }

   public function getQuestion() {

       return $this->question;

   }

}

Advantages of Dependency Injection

I have read about many stories of the development of code for large scale projects. One such story is of a company(Anonymous) that requested the application in 2009. The project’s budget was properly planned, but the project manager wanted to impress some of his seniors and wanted to develop the application as quickly as possible. By reducing the estimated time required to develop the application, the overall costs come down. But, it also meant that the developers jumped right into writing the code without any planning.

 

The developers wrote the code quickly but, creating classes as necessary to perform the necessary functionality of the application. But, the classes were so tightly coupled, it became a horrific experience for the coders to make any modifications in the application as the app was poorly planned. Modifying a simple class caused a ripple effect to other parts of the code and hence as soon as any changes were made to the code, the job of testing people was shot up to fix the bugs in the other parts of the code.

 

The Advantages of dependency injection could be understood as:

 

  • You should definitely look at using the dependency injection during the development of long-term projects.
  • The dependency injection eases the unit testing, hence the code quality is at its best.

 

Parmod KumarParmod is a web development veteran with a vast expertise of working on industry-leading technologies like Wordpress, JQuery, Javascript, CSS, and PHP. When he’s not busy coding the next big thing, you will find him reading about the latest and greatest in the development industry.

Keeping himself abreast and staying on top of the latest development trends is what he loves the most and his astounding work is a pure testament of that.