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

WordPress Trac noreply at wordpress.org
Wed Aug 17 20:49:30 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          |   Keywords:
  Focuses:                  |
----------------------------+-----------------
 Storing application state in globals is ... bad.
 Using global state to store objects is ... bad.
 Using globals to avoid writing classes is ... bad.
 Using globals to pass data between functions is ... bad.
 Training people to assume that globals will always be set to the value
 they expect is ... bad.

 Globals are an artifact of a #dark-er time, think PHP 3. It would be nice
 if we didn't use globals. We have a backward compatibility strait jacket,
 but that shouldn't stop us from exploring ways to eradicate them.

 We can start with class instances, because variables hold a reference to
 the actual instance when set. We can also start with a simple registry
 that allows us to create a data that can get / set globals for us (or
 not!). We can also add a few static methods onto `WP` to hide all of this
 away.

 Attached is a POC that removes (every instance of?) `$wpdb` as a global,
 and instead uses the registry. Take a glance. All unit tests (seem to)
 pass.

 This will evolve before it becomes a reality, but I encourage you to
 unleash your imagination unto a world where WordPress' codecase resembles
 something globallessly lovely.

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


More information about the wp-trac mailing list