Ticket #305 (new defect)
Replace use of static methods with proper dependency injection
| Reported by: | agilbert | Owned by: | tboutell |
|---|---|---|---|
| Priority: | major | Milestone: | 2.0 |
| Component: | apostrophePlugin | Version: | trunk |
| Keywords: | Cc: | dordille | |
| Symfony version: | 1.4 |
Description
http://www.phparch.com/2010/03/03/static-methods-vs-singletons-choose-neither/
Static methods can quickly lead to inflexibility and other problems. Specifically, static methods are very difficult to override and therefore nearly impossible to test. Sebastian Bergmann addresses this very issue in a recent blog post: Stubbing and Mocking Static Methods. The quote from Misko Hevery is particularly meaningful:
Yes, static methods are easy to call, but if the static method calls another static method there is no way to override the called method dependency.
Apostrophe's core code is full of static method calls. It has become our habit that if we need some new special functionality, we stick it in a static class and then we can call it wherever.
If we were to have properly adhered to the symfony convention with 1.0, we would have used an apostrophe singleton for this stuff, i.e.
$a = apostrophe::getContext();
As the article above points out, Symfony 2.0 is moving towards proper dependency injection. By the time Apostrophe is running on Symfony 2.0, we need to get rid of this practice.

