Home arrow PHP arrow The Dependency Injection Design Pattern in PHP 5

The Dependency Injection Design Pattern in PHP 5

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.

  1. The Dependency Injection Design Pattern in PHP 5
  2. A basic MySQL-driven application with the dependency injection pattern
  3. Defining a basic factory method
  4. Changing the definition of the User class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 5
October 08, 2009

print this article



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: