To participate you must create an account on apostrophenow.org. If you have already done so, click Login.

Ticket #305 (new defect)

Opened 22 months ago

Last modified 17 months ago

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.

Change History

Changed 22 months ago by agilbert

  • summary changed from Replace use static methods with proper dependency injection to Replace use of static methods with proper dependency injection

Changed 17 months ago by tboutell

  • owner changed from boutell to tboutell
Note: See TracTickets for help on using tickets.