Dependency Injection

Oct 09, 2017 PHP
Dependency Injection

Dependency Injection is one of the simple PHP design pattern I know. It makes a lot sense when using with object oriented programming. It provides the depended object through constuctor, method calls or setting of properties.

It would be more easier to understand using an example. Let say you have a blog application where you have a class called post as shown below:

class Post {

    private $type;
 
    public function __construct($type) {
        $this->type = $type;
    }
 
    public function getPostType() {
        return $this->type;
    }
}

// creating image post
$imagePost = new Post('image');
$videoPost = new Post('video');

Your class looks fine at this moment, it provides the depeneded post type through constructor. We can call this as a dependency injection however it make more sense if we use object oriented approach.

Let's create image/video type to it's own class:

class ImagePost {

}

class VideoPost {

}

// create image post
$imagePost = new ImagePost();
$postImage = Post($imagePost);

// create video post
$videoPost = new VideoPost();
$postVideo = Post($videoPost);

Well, now that we split the post types and created their own classes and we pass the type object to Post contructor. Here our Post class constructor takes $type argument however we are not sure what it would be unless we know incoming postype object:

Our code does not break at this point because __construct($type) is still valid and we are injecting depended object using Post constructor. Now, let's implement php design pattern and make our code more efficient. I will create an interface called PostTypeInterface as shown below:

interface PostTypeInterface {}

Now, we will implement our post type classes using this new interface as shown below:

class ImagePost implements PostTypeInterface {

}

class VideoPost implements PostTypeInterface {

}

// modifying post constructor
class Post {

    private $type;
 
    public function __construct(PostTypeInterface $type) {
        $this->type = $type;
    }
 
    public function getPostType() {
        return $this->type;
    }
}

This is a good example of dependency injection, you can have number of different post type classes that can implement PostTypeInterface and when your application grows your post class defination [__construct(PostTypeInterface $type)] does not change and will not break.

Supplying PostType classes through Post constructor as a dependecy called depenency injection and it makes lot sense when using object oriented approach.

The idea here is that your post type classes for example ImagePost or VideoPost can have different functions or functionality and using interface you can force all post types classes to implement common functions. without affecting post class:

interface PostTypeInterface {
   public function show();
}

class ImagePost implements PostTypeInterface {
  public function show() {
     // show image related functionality
  }
}

class VideoPost implements PostTypeInterface {
  public function show() {
     // show video related functionality
  }
}

I hope you enjoyed my tutorial about dependecy injection and will have some comments for me in comment section. I could be wrong at explaing it correctly however this is a best approach to use dependency injection in PHP.

Rate This Article

Dependency Injection (DI) is a software design pattern that deals with how components get hold of their dependencies.

4.60

About Author