PhpRiot
News Archive
PhpRiot Newsletter
Your Email Address:

More information

Stubbles Blog: Dependency Injection for static methods

Note: This article was originally published at PHPDeveloper on 9 November 2010.
PHPDeveloper

On the Stubbles blog today there's a new post from Frank Kleine showing how you can use a new feature of PHP 5.3 to perform dependency injection on static method - calling static methods with dynamic class names.

Developers who are familiar with the topic of clean code know that static methods are a problem, especially if it comes to testability. Static makes the code global, and if there is some kind of state involved it is even more a problem because this is global state then - both a maintenance and testability nightmare. [...] How can we keep our code clean and testable, at best without having a strongly coupled dependency to the class hosting the static method? Enter dependency injection for static methods.

He gives an example of how to use this to create a simple class that can change the name of the object an inner static function call is made to.