[wp-trac] [WordPress Trac] #25160: Unnecessarily passing $this by reference in core

WordPress Trac noreply at wordpress.org
Sun Jan 4 21:48:12 UTC 2015


#25160: Unnecessarily passing $this by reference in core
-------------------------------------------------+-------------------------
 Reporter:  jdgrimes                             |       Owner:
     Type:  defect (bug)                         |      Status:  new
 Priority:  normal                               |   Milestone:  Future
Component:  General                              |  Release
 Severity:  normal                               |     Version:
 Keywords:  has-patch needs-testing needs-codex  |  Resolution:
  3.9-early                                      |     Focuses:
                                                 |  performance
-------------------------------------------------+-------------------------

Comment (by jdgrimes):

 Replying to [comment:22 wonderboymusic]:
 > In [attachment:25160.diff], I think these are all safe, but will sit on
 it for now.
 >
 > `do_action_ref_array( 'action', array( &$this ) );`
 >
 > becomes `do_action( 'action', $this );`
 >
 > Only touches actions that pass one arg: `$this`

 I really wish we could do this, but I don't think the patch is safe as is.
 It will cause warnings to be thrown. There is detail as to why this is in
 #16661 as well as
 [https://core.trac.wordpress.org/ticket/25160?replyto=22#comment:17
 above].

 There is even at least one example of this in core itself. The
 [https://core.trac.wordpress.org/browser/trunk//src/wp-includes/script-
 loader.php#L50 wp_default_scripts()] function, which is hooked to the
 `'wp_default_scripts'` action is an example.

 Test code:

 {{{
 call_user_func_array( 'wp_default_scripts', array( $GLOBALS["wp_scripts"]
 ) );
 }}}

 This will result in:

 {{{
 PHP Warning:  Parameter 1 to wp_default_scripts() expected to be a
 reference, value given in...
 }}}

 Interestingly, this doesn't give an error:

 {{{
 $func = 'wp_default_scripts';
 $func( $GLOBALS["wp_scripts"] );
 }}}

 The warning only appears when using `call_user_func_array()` (at least on
 PHP 5.6.3). This may provide an out, but to take advantage of it we would
 have to modify `do_action()` and `apply_filters()` not to use
 `call_user_func_array()`. Thoughts?

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


More information about the wp-trac mailing list