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 Hall of Famer's Avatar
    Join Date
    Apr 2013
    Location
    Ithaca
    Posts
    370
    Mentioned
    6 Post(s)
    Tagged
    2 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

  4. #4
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,446
    Mentioned
    45 Post(s)
    Tagged
    13 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,182
    Mentioned
    67 Post(s)
    Tagged
    2 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
    439
    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
  •