Diving into the power of Dependency Injection Pattern in PHP


Dependency Injection in PHP


Introduction

In my previous article I shown you "How to build a simple form using Cygnite PHP Framework with Mysql and Angular Js". Today I have an Interesting topic.

Dependency Injection? Sounds Interesting. Yes! Currently it is one of the raising discussion topics among many PHP developers and communities. It is not a new pattern however commonly used in language like java etc. These concepts are mostly used in MVC (Symfony2, Cygnite Framework, Zend2, Laravel etc.) architecture to decouple code. It is because PHP has been adapted to enterprise level and that is why increasing the demand of quality programming with some patterns. If you already know concept of Dependency Injection then you may simply skip the article and visit us back for other latest posts.

Well! What is Dependency Injection?

It is a simple design pattern to create most decoupled software. It makes your code testable, modular and maintainable.

Advantages of Dependency Injection

• Makes your code Testable, maintainable and modular.
• Gives you to flexibility to change dependencies at runtime.
• Allows you building large and scalable projects.

You might have used this pattern unknowingly. So let us see some example how dependency injection are best practice.


class DataGrid
{
    public function render($data)
 {
     ..................
 } 
}

class Application 
{
    private $connection;

    private $session;
 
    public function __construct($session, $host, $username, $password)
    {
          $this->db = new MySqlDatabase($host, $username, $password);
          $this->session = $session; 
    }
 

   public function renderGrid()
   {
          $grid = new DataGrid;
          return $grid->render($this->db->fetchAll());
   } 
}

So in the above example we can see Foo class is dependent on MysqlDatabase class which is hard coded and tightly coupled with each other. Many people says above code is good but if you closely have a look above code has many problem. It uses S.O.L.I.D principle.

What If you want to use some other database class, may be Oracle or any other in future?

What if you want to access extra parameter inside your MysqlDatabase class?

Why to avoid above code?

• Let’s image if we have added extra parameter to MysqlDatabase class, then we need to modify each and every class where MysqlDatabase object created.
• Doing unit testing is hard because all classes are tightly coupled.

How to solve this issue?

Dependency Injection is the solution. There are different types of Injection you can take the advantage of.

• Constructor Injection.
• Setter or Property Injection.
• Interface Injection etc.

Let us rewrite the above code with DI pattern



class Application 
{
    private $connection;
   
    private $grid;

    private $session;
 
    public function __construct($session, MySqlDatabase $database, DataGrid $grid)
    {
          $this->db =  $database;
          $this->session = $session;
          $this->grid = $grid;  
    } 

   public function renderGrid()
   {
          return $this->grid->render($this->db->fetchAll());
   } 
}

$app = new Application($sessionName, new MysqlDatabase(), new DataGrid());
$app->renderGrid();


You can see in the above example we have reduced the complexity and improved it by decoupling it. We have done constructor injection with Type Hinting. It is easy to do unit test now for each class.

You also inject dependencie using property injection or Interface injection. You may have a look at Cygnite Framework DI container, it resolves controller dependencies without any configuration.

Over using DI pattern into your code may lead you to messy code.

When to use Dependency Injection?

Actually it is not when to use Dependency Injection, you can use it for writing more clean code. However Dependency Injection is most preferable in long term projects. It attracts the best developers to follow your code.

Open Source Dependency Injection Frameworks

These days DI became very popular to PHP developers and many framework uses DI container to resolve Dependencies of your Controller class. Some of them are listed below-

Symfony2 Container based on Java Spring.
Cygnite Framework DI Container.
Zend2 DI.
Laravel IoC etc.

Conclusion

So at the end I will say use the pattern wherever applicable, it should not overkill your smaller project. Hope the above article is useful to you and if you have any real time stories to share about Dependency Injection please don't forget to leave a comment below, also give a small like or share. Keep visiting for latest posts.

Follow Us On Facebook Open Source Web Developers by Appsntech facebook group Twitter Open Source Web Developers by Appsntech twitter group Google+ Open Source Web Developers by Appsntech Google group Linkedin Open Source Web Developers by Appsntech, LinkedIn group
Copyright @2011-2015 appsntech.com. All rights reserved. Powered By- Sanjoy Dey Productions