PHP
  Home arrow PHP arrow The Dependency Injection Design Pattern in PHP 5
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

The Dependency Injection Design Pattern in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2009-10-08


    Table of Contents:
  • The Dependency Injection Design Pattern in PHP 5
  • A basic MySQL-driven application with the dependency injection pattern
  • Defining a basic factory method
  • Changing the definition of the User class

  • 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


    The Dependency Injection Design Pattern in PHP 5
    ( Page 1 of 4 )

    In this first part of a six-part series, I introduce you to the dependency injection design pattern and its use with MySQL. Specifically, I create a typical scenario where one persistent class needs the functionality of its dependency, in this case a database handler, to gain access to a MySQL table.

    Without a doubt, design patterns are one of the big pillars of software development that allow you to build high-quality desktop and web applications through well-trusted solutions. A solid understanding of basic concepts such as inheritance and polymorphism, in conjunction with a respectable knowledge of the most popular design patterns, can be of great help in developing professional web-based programs.

    Naturally, some design patterns are harder to master than others. A few of them, however, are remarkably simple to implement in real-world conditions, and their benefits can be really enormous.

    In the particular case of PHP 5, one of easiest patterns that can be applied to improve the quality of object-oriented applications is one called Dependency Injection, even though it’s not as well known as others. As its name suggests, the Dependency Injection pattern permits you to define how a given class is going to accept its dependencies, or in other words, the way that the instances that a class needs to work will be injected into its internals.

    To clarify a bit further, I’m going to cite a typical example that will be familiar to you. Say there’s a model class that needs to take in an instance of a database handler to perform queries against its associated database table. In a case like this, the database handler would obviously be a dependency of the model, and it could be passed in to the model’s environment either via the constructor, or via a setter method.

    This would be a basic – yet realistic -- example, where the Dependency Injection pattern is applied to promote encapsulation. Logically, this pattern can be implemented in several ways. In this series of articles I’m going to cover only some of them, including naturally the most relevant ones. In addition, I plan to touch superficially the construction of dependency injection containers, a subject that itself deserves an in-depth look.

    Now, it’s time to leave the theory the drives the Dependency Injection pattern behind and start learning how to apply it in PHP 5-driven environments. Let’s get started!



     
     
    >>> 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 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek