PHP
  Home arrow PHP arrow Dependency Injection: Using a Setter Method within a Model
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

Dependency Injection: Using a Setter Method within a Model
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2009-10-19


    Table of Contents:
  • Dependency Injection: Using a Setter Method within a Model
  • Review: the Model-View-Controller and Dependency Injection Patterns
  • Applying dependency injection through a setter method
  • Performing CRUD operations on user-related records

  • 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


    Dependency Injection: Using a Setter Method within a Model
    ( Page 1 of 4 )

    In this fifth part of a six-part series on the dependency injection pattern, I explore the implementation of the pattern by using a simple setter method within a model class. You'll see that this approach is very easy to follow.

    While not as widely known as other popular design patterns like Singleton and Factory, the dependency injection pattern is a powerful programming methodology that allows you to define the way that one given class is going to accept additional objects that it requires to work as expected.

    In the case of PHP 5, there are two major trends that most developers stick to when it comes to implementing dependency injection within a certain application: the first one relies on injecting the dependency of a class via its constructor, while the second approach does this, but by means of a setter method instead. Of course, both approaches use aggregation to deal with a class’s dependency, since in this case, composition contradicts the schema imposed by the Inversion of Control principle.

    All of these buzzwords should be pretty familiar to you at this point, because over the course of the tutorials that preceded this one I explained how to apply the dependency injection pattern. If you go back to the previous part of this series, then you’ll recall that I left off discussing how to use the “injection by constructor” approach with a couple of basic classes.

    In this specific example, the first of these classes was a simple MySQL handler, while the second one was a model class that required the functionality of the first to interact with a predefined MySQL table. This fictional scenario showed that it’s very easy to combine the power of dependency injection with the benefits of the Model-View-Controller pattern within a single application.

    However, as I said a moment ago, dependency injection can also be achieved with a setter method. So, in this installment I’m going to explain how to use this approach with the pair of sample classes previously mentioned.

    Want to see how this will be done? Then begin reading!



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

       

    PHP ARTICLES

    - Merging a File Split for FTP Upload using PHP
    - 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 ...





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