March 17
Inversion of Control/Dependency Injection with PHP
by David Palmer
Over the last several years the design paradigm of inversion of control/dependency injection has gone from an interesting design exercise to major league time savings and making complex things less complex (and at the same when done wrong, making complex things even more complex.) Developers in the Java world have enjoyed Spring, the de facto library that makes all of the promises of IoC/DI possible and rather straight forward.
However, with the continual maturity of OOP in PHP the concept of inversion of control/dependency injection has been slow to catch on. There have been attempts at providing IoC/DI functionality to PHP but many have just withered away and died. One such framework that may show some significant promise is Flow3, produced by the community of developers who have been slaving away at Typo3, a long running, incredibly feature-rich open source enterprise CMS. However, to date, Flow3 is very much like vaporware. Oh sure you can download it, but documentation is scarce to non existent and its missing one thing that I believe would slow its adoption, its similarity to Spring.
Spring got it right, by my estimation. The notion of wiring an application together via an XML file is spot on and truly enables developers to “write to interfaces” without ever having to concern themselves with the concrete implementations. The better code design, testing and time savings become very apparent once fully in practice. This shouldn’t just be something that Java (or .NET) developers enjoy. PHP is a major piece of technology, so with that said, I’ve taken the time and out of a graduate school project I recently finished devised a rudimentary IoC/DI container written in PHP. Currently it only supports a limited set of features, but uses the same concepts as used by Spring. Enter, phpBoing, which right now is quite simple and is just an academic curiosity, but with some work it could grow into a full fledged mature application development framework. No it is NOT meant to be another MVC framework. There are too many of those already, in fact, this system simply provides an object registry that lives in memory, defined through an “application context” XML where you can “inject” object dependencies via XML syntax, thus enabling you to “wire” your PHP applications together in XML (as opposed to doing so in actual PHP code).
In invite you to take a look, stab around at it, and if so inclined, contribute code.

The Inversion of Control/Dependency Injection with PHP by Molecular Voices, unless otherwise expressly stated, is licensed under a Creative Commons Attribution 3.0 Unported License.