PHP
  Home arrow PHP arrow Page 4 - Injecting Objects by Constructor with the Dependency Injection Pattern
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Forums Sitemap 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Injecting Objects by Constructor with the Dependency Injection Pattern
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 1
    2009-10-13


    Table of Contents:
  • Injecting Objects by Constructor with the Dependency Injection Pattern
  • Review: inefficiently handling class dependencies
  • Applying the Inversion of Control principle
  • The dependency injection pattern in action

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Injecting Objects by Constructor with the Dependency Injection Pattern - The dependency injection pattern in action
    (Page 4 of 4 )

    As I anticipated in the previous segment, the only thing that remains undone here is building a script that shows how to create a couple of persisting objects, after improving the definition of the originating class.

    With that idea in mind, I wrote that script, and it appears below. Take a look at the following fragment, please:

    // example of dependency injection via the constructor

    // create instance of MySQL class

    $db = new MySQL('host', 'user', 'password', 'database');

    // create first user object

    $user1 = new User($db);

    $user1->name = 'Alejandro Gervasio';

    $user1->email = 'alejandro@domain.com';

     

    // create second user object

    $user2 = new User($db);

    $user2->name = 'Mary Smith';

    $user2->email = 'mary@domain.com';

    Simple to code and read, isn’t it? Quite possibly, the previous script isn’t going to change the way that you develop your PHP 5 applications forever, but it shows in a nutshell how beneficial dependency injection can be when it comes to building programs where the context is responsible for providing the required dependencies to its objects, and not the opposite.

    And with last code sample, I’m finishing this second episode of the series on applying the Dependency Injection pattern in PHP 5. As usual, feel free to edit all of the examples included in this tutorial, so you can develop a better understanding on this rather overlooked pattern.

    Final thoughts

    Over this second part of the series, you learned how to implement a specific version of the dependency injection pattern, commonly called “injection by constructor.” As its name suggests, this approach injects the dependency directly through the constructor of the receiver object. It’s just that simple.

    In the next installment, I’m going to teach you how to apply the Dependency Injection pattern in its other flavor, which simply passes the corresponding dependency via a setter method.

    Don’t miss the upcoming part!



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Getting More Info on Reflected Properties wi...
    - Implementing the Iterator SPL Interface
    - Building a Data Access Layer for the Data Ma...
    - Building a Singleton Database with Restricti...
    - Working with Reflected Properties with the R...
    - The Iterator, Countable and ArrayAccess SPL ...
    - Implementing the Data Mapper Design Pattern ...
    - Defining an Abstract Class with Restrictive ...
    - The Reflection API: Working with Reflected M...
    - Using Restrictive Constructors in PHP 5
    - Getting Information on a Reflected Class wit...
    - Introducing the Reflection API in PHP 5
    - Swift Mailer's Batchsend Method and Other Fe...
    - Embedding Attachments into Email Messages wi...
    - Dynamically Attaching Files with Swift Mailer


    Code Analysis Tools
    Enterprise code analysis tools that deliver quality and reliable code



    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 9 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek