[wp-trac] [WordPress Trac] #37699: Death to Globals Episode #1: A Registry, A Pattern

WordPress Trac noreply at wordpress.org
Thu Aug 18 17:38:17 UTC 2016


#37699: Death to Globals Episode #1: A Registry, A Pattern
----------------------------+------------------
 Reporter:  wonderboymusic  |       Owner:
     Type:  enhancement     |      Status:  new
 Priority:  normal          |   Milestone:  4.7
Component:  General         |     Version:
 Severity:  normal          |  Resolution:
 Keywords:                  |     Focuses:
----------------------------+------------------

Comment (by charliespider):

 building on schlessera's comments,

 I think a better solution to a Registry or Service Locator would be to
 just do things properly and use a Dependency Injection Container...

 BUT...

 when replacing globals, do '''NOT''' use the DI container directly for
 obtaining the class you want (like wpdb), because that would basically be
 service location, which is not a good way to go about things and
 considered an anti-pattern by some, but instead use a static factory
 class. Then your factory class, can have access to the DI container, which
 at least minimizes your use of service location to specific classes as
 well as minimizing the scope of the service location (since a db
 connection factory '''only''' ever returns a db connection, as opposed to
 being used for locating any number of services).

 Over time, as procedural code gets replaced with OOP, use of these
 factories can be dropped in favour of injecting a class's required
 dependencies upon construction.

 So the use of factories for specific service location is a stepping stone
 to proper dependency injection, with the end goal being dependency
 inversion, and the adoption of modern best practices.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/37699#comment:14>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list