[wp-trac] [WordPress Trac] #45374: apply_filters_ref_array() no longer passes arguments by reference

WordPress Trac noreply at wordpress.org
Wed Dec 5 15:29:42 UTC 2018


#45374: apply_filters_ref_array() no longer passes arguments by reference
------------------------------------------+------------------------------
 Reporter:  johnbillion                   |       Owner:  (none)
     Type:  defect (bug)                  |      Status:  new
 Priority:  normal                        |   Milestone:  Awaiting Review
Component:  Plugins                       |     Version:  4.7
 Severity:  normal                        |  Resolution:
 Keywords:  needs-patch needs-unit-tests  |     Focuses:
------------------------------------------+------------------------------

Comment (by websupporter):

 Since its almost end of the work day, I leave what I found so far.

 This is the code I've tested with
 {{{#!php
 <?php
 function testFilter( $text, &$object ) {
     return $text;
 }
 add_filter( 'testFilter', 'testFilter', 10, 2 );

 function testAction( $text, &$object ) {
     return;
 }
 add_action( 'testAction', 'testAction', 10, 2 );

 class Test {

     public function run() {
         // Somehow the &$this does not work.
         apply_filters_ref_array('testFilter', ['test', &$this]);
         apply_filters_ref_array('testFilter', array('Test', &$this));

         // It seems to be limited to filter.
         do_action_ref_array('testAction', array('test', &$this));
     }
 }
 (new Test())->run();

 /**
  * These do not throw.
  */
 $object = new Test();
 apply_filters_ref_array('posts_clauses2', ['test', $object]);
 apply_filters_ref_array('posts_clauses2', ['test', &$object]);
 }}}

 It seems to be related to calls where the referenced object is `$this`.
 The PHP version I've tested is 7.2.12-1.

 It looks to me, it has something to do with the reassignment of the first
 value of the `$args` here
 https://core.trac.wordpress.org/browser/tags/4.9.8/src/wp-includes/class-
 wp-hook.php#L279

 When I just do not run this assignment, the problem disappears.

 But, at least for PHP 7.1.25 the problem seems to be a bit more:
 https://3v4l.org/DCCeh

 As you can see, here `$this` is not referenced, although I do ''not''
 change the `$args`. Also noteworthy, in this example I can change
 `$args[0]` without having another problem (except for the PHP 7.1.25).

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


More information about the wp-trac mailing list