PHP
  Home arrow PHP arrow 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
    ( Page 1 of 4 )

    While not being as widely known as other design patterns such as Factory, Singleton or Active Record, the Dependency Injection pattern provides both programmers and web developers with a well-structured solution that allows them to solve issues that arise when an object needs the functionality of another one (hence the dependency) to work as expected. This is the second part of a six-part series that shows you how to apply this pattern.

    In simple terms, when this pattern is applied, the dependency is directly injected (hence its name), either through the constructor of the receiver object or via a setter method. In both cases, the implementation of the pattern goes hand in hand with a concept of software engineering called Inversion of Control. This concept states that the context in which an object exists is responsible for providing it with its dependencies, and not the inverse situation (the term “inversion” makes sense here).

    Obviously, in a scenario where objects are merely passive entities that expect to receive the dependencies that they need to function properly from the outer world, putting injection dependency into practice is a no-brainer process. This is particularly true in the case of PHP 5 where its type hinting feature helps to achieve this task in a simple manner.

    If you've already read the first part of this series, then you should have a vague idea of how to implement the dependency injection pattern in PHP 5. In that tutorial I developed a sample application composed of two building blocks: a rudimentary MySQL abstraction class, and a persistent class that required the functionality of the first for saving and retrieving its properties from a database table.

    In this specific example, persistent objects were entirely responsible for creating the database handler that they needed to work, which happens to contradict the Inversion of Control principle. This is not a desirable effect.

    Thus, in this second part of the series I’m going to discuss how to use the dependency injection pattern to solve the aforementioned issue. Now, it’s example time, so let’s move on and start writing some functional PHP 5 code!



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

       

    PHP ARTICLES

    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5





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