PHP
  Home arrow PHP arrow Injecting Objects Using Setter Methods with the Dependency Injection Design 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 Using Setter Methods with the Dependency Injection Design Pattern
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 1
    2009-10-14


    Table of Contents:
  • Injecting Objects Using Setter Methods with the Dependency Injection Design Pattern
  • Review: implementing the dependency injection pattern using a constructor method
  • Using dependency injection via a setter method
  • 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 Using Setter Methods with the Dependency Injection Design Pattern
    ( Page 1 of 4 )

    In this third article of a six-part series, you will learn how to implement the dependency injection pattern by using a simple setter method. This process is very similar to the one that uses a constructor, which we covered in the previous article.

    One of the most common tasks that many PHP 5 programmers must tackle during the development of object-oriented web applications is defining the way that objects are going to interact with each other. While this process seems to be a no-brainer one at first sight, the truth is that in real-world conditions it can be an extremely challenging and daunting experience.

    Sad but true, a small application initially conceived to use some scarce classes can quickly become a confusing, messy maze as it grows in size and complexity, where certain objects are responsible for creating other objects here and there, without any type of restriction or predefined order. If you’ve ever reached that point as a PHP developer, then you know that you’ve been knocking on hell’s doors.

    Fortunately, there’s a proven solution for a situation like this, which is incredibly simple to implement, called dependency injection. As the name suggests, this pattern permits you to define, through a few straightforward approaches, the way that classes will take in their dependencies, without compromising class encapsulation.

    Due to the schema imposed by this pattern, it actively promotes the application of the Inversion of Control principle. This principle dictates that the environment is always responsible for providing objects with their dependencies, and not the other way around.

    Of course, if you already read the previous part of this series, then you have a clearer idea on how to use dependency injection when working with a database handler and a class that needs it to build persistent objects.

    In that example in particular, the database handler (read the dependency) was passed to the persistent class via its constructor method, which is one of the simplest ways to implement the pattern. Though, as I said before, dependency injection can be accomplished by using several approaches, including one that uses a setter method to pass the dependency of a given object.

    In the next few lines I’m going to explain how to translate this last approach to functional PHP 5 code. Now, let’s continue exploring the benefits of using the dependency injection pattern. Let’s go!



     
     
    >>> 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