php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75148 Changing array entry including &$this causes warning with call_user_func_array
Submitted: 2017-09-01 21:54 UTC Modified: 2020-12-07 12:00 UTC
From: robin at ambitionally dot com Assigned:
Status: Open Package: Scripting Engine problem
PHP Version: 7.1.9 OS: Windows, Linux
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: robin at ambitionally dot com
New email:
PHP Version: OS:

 

 [2017-09-01 21:54 UTC] robin at ambitionally dot com
Description:
------------
When modifying an argument array that includes an entry for &$this, the assignment operation seems to de-reference the &$this argument. As shown in the test script, line 6 and line 8 are identical, and line 7 assigns the same value to index [0] of the array.

The issue is specific to PHP 7.1 (I have tested with 7.1.8 and 7.1.9), but it does not appear in PHP 7.0.

var_dump before and after the assignment operation has the same printout. There is a difference in var_dump output between PHP 7.0 and 7.1. In 7.1, the var_dump output is missing the "&" for array entry [1], while the "&" symbol is present in PHP 7.0.

This issue causes problems for WordPress sites, as it uses similar logic when running filters (WP_Hook::apply_filters).

Test script:
---------------
<?php
function test_call($first, &$a) {
  echo '<div>here</div>';
}
function test_warning($args) {
  call_user_func_array( 'test_call', $args ); // this line works
  $args[ 0 ] = 1;
  call_user_func_array( 'test_call', $args ); // this line causes error
}
class TestWarningClass{
  function __construct() {
      $args = array( 1, &$this );
      test_warning($args);
  }
}
$test = new TestWarningClass;

Expected result:
----------------
No warning. Two lines of "here" are printed.

Actual result:
--------------
Warning: Parameter 2 to test_call() expected to be a reference, value given

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-09-04 22:24 UTC] cmb@php.net
-Package: Class/Object related +Package: Scripting Engine problem
 [2020-12-07 12:00 UTC] cmb@php.net
I think this is related to the changes to call_user_func()
handling of reference arguments[1], and likely something that
won't be fixed.  Use variable functions and the splat operator
instead[2].

[1] <https://www.php.net/manual/en/migration71.incompatible.php#migration71.incompatible.call_user_func-with-ref-args>
[2] <https://3v4l.org/MDLLi>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 12:01:31 2024 UTC