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

WordPress Trac noreply at wordpress.org
Thu Aug 18 18:22:02 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):

 Replying to [comment:15 wonderboymusic]:

 instead of :

 {{{#!php
 <?php
 // src/wp-includes/class-wp-comment-query.php
     public function __construct( $query = '' ) {
         this->db = $GLOBALS['wpdb'];
 }}}

 why not:

 {{{#!php
 <?php
 // src/wp-includes/class-wp-comment-query.php
     public function __construct( $query = '', wpdb $wpdb = null ) {
         this->db = $wpdb instanceof wpdb ? $wpdb : $GLOBALS['wpdb'];
 }}}

 and then when constructing that class:


 {{{#!php
 <?php
 // if we still NEED to grab the global
 global $wpdb
 new WP_Comment_Query( $comment_args, $wpdb );
 // else use an already injected instance (assuming $this->wpdb ===
 $GLOBALS['wpdb'])
 new WP_Comment_Query( $comment_args, $this->wpdb );
 }}}

 this would be a better first step towards proper dependency injection

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


More information about the wp-trac mailing list