SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Sep 2013
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Dependency Injection

    Hello, I have a simple question...
    Why dependency injection is used with OOP?
    Can anyone clear this out..

  2. #2
    SitePoint Addict bronze trophy
    Join Date
    Apr 2013
    Location
    Ithaca
    Posts
    338
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)
    Because you have objects that depend on the presence of other objects but you don't want to make these objects global variables. Dependency injection is a great alternative to global variables and singleton statics, both are anti-patterns to avoid as an aspiring OO programmer.

    One common usage for dependency injection is when you have a domain model or data mapper that does CRUD operations, which will likely to require a database object(like a PDO instance). Without dependency injection, you either have to create such a database object in the constructor of the domain object, or use one database object as global variable. The former can lead to performance issues when you instantiate a collection of domain objects, the latter is a bad practice to avoid. By using dependency injection, you pass an instance of database object to domain object's constructor, it does not create extra copies of database object, nor is it using any globals. In this way, you write more elegant, maintenable and unit-test friendly code.

  3. #3
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,813
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Well, you may want to start with reading these threads:
    http://www.sitepoint.com/forums/show...ency+injection
    Helps Simplify Code - http://www.sitepoint.com/forums/show...ency+injection
    Did I use DI correctly? - http://www.sitepoint.com/forums/show...ency+injection
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  4. #4
    SitePoint Mentor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,257
    Mentioned
    32 Post(s)
    Tagged
    5 Thread(s)
    To expand a little on what Hall of Famer said, have a look at the examples below. It's common to see code like the first example where dependencies are hard-coded into the class:

    PHP Code:
    class User
    {
        public function 
    __construct()
        {
            
    $this->db = new PDO(...);
        }
        
        
    // ...
    }

    $user = new User
    Here's how you might rewrite the class to utilise DI:

    PHP Code:
    class User
    {
        protected 
    $db;
        
        public function 
    __construct(PDO $db)
        {
            
    $this->db $db;
        }
        
        
    // ...
    }

    $user = new User(new PDO(...)); 
    By allowing the PDO dependency to be injected via the constructor, the User class is now more flexible: we could instantiate a user object which writes it's data to a different DB just by swapping out the PDO connection that we pass in. It's also easier to test, as we can pass in a mock DB connection.

  5. #5
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,079
    Mentioned
    53 Post(s)
    Tagged
    0 Thread(s)
    Really, a better example would be this:

    PHP Code:
    class User
    {
        protected 
    $db;
        
        public function 
    __construct(PDO $db)
        {
            
    $this->db $db;
        }
        
        
    // ...
    }
    $PDO = new PDO(...);
    $PDO->dosomething();
    $user = new User($PDO);
    $PDO->dosomething(); 
    Both $user class, and $PDO->dosomething() is altering and using the same variable, in this case, using the same connection. If I were to have a class where dosomething() were simply incrementing a vlaue inside the class, and dosomething() were called both in $user as well as outside $user, the value would be equivalent to how many times it was called.

  6. #6
    SitePoint Member
    Join Date
    Sep 2013
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you of of you.. Iv been able to understand it.. ty..

  7. #7
    SitePoint Evangelist
    Join Date
    May 2006
    Posts
    423
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •