php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #73238 call_user_func() doesn't pass-by-ref; variable functions do
Submitted: 2016-10-04 01:10 UTC Modified: 2021-09-22 11:45 UTC
From: yohgaki@php.net Assigned: cmb (profile)
Status: Closed Package: Unknown/Other Function
PHP Version: 7.0.11 OS: Irrelevant
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: yohgaki@php.net
New email:
PHP Version: OS:

 

 [2016-10-04 01:10 UTC] yohgaki@php.net
Description:
------------
As you can see, newer PHPs have additional errors with call_user_func().

https://3v4l.org/oBnkT

Warning: Parameter 1 to foo() expected to be a reference, value given in /in/oBnkT on line 10

Warning: Parameter 1 to foo() expected to be a reference, value given in /in/oBnkT on line 11


I found this errors from Wordpress installation.
https://wordpress.org/support/topic/wp-4-6-1-got-error-with-php-7-1/


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-10-10 22:10 UTC] yohgaki@php.net
More test code
https://3v4l.org/TZaVe (array)
https://3v4l.org/WMMQa (string)
https://3v4l.org/OEOA1 (return value check)

Removing error from call_user_func('foo', $value), when $value is object, is preferred. foo() could be.

function foo(&$value) {
  if (is_string($value)) {
      $value = 'update $value';
  } else if (is_object($value)) {
      $value->some_var = 'add some_var';
  } else {
      trigger_error('You should pass string or object');
  }
}
 [2016-10-10 22:15 UTC] nikic@php.net
These changes are intentional -- they bring the call_user_func VM implementation in line with the actual function. call_user_func() categorically cannot pass references, so you get a warning. (At least this is part of it.)

However, in PHP 7.1 we have changed call_user_func() to still proceed with the call even if such a warning occurs. Previously the call would be aborted (which would be a BC break). Now it will continue as normal, only with the warning.
 [2016-10-10 23:00 UTC] yohgaki@php.net
@nikic
I agree that errors should be raised for bogus calls like

call_user_func('foo', 'abc'); // Literal cannot be reference
call_user_func('foo', [$o]); // There is no modifiable value
call_user_func('foo', array_slice($a, 0, 1)); // There is no modifiable value

when foo() is

function foo(&$value) { ... }

These call_user_func() calls cannot be pass by reference. I'll update http://php.net/manual/en/function.call-user-func.php and others to document behavior precisely.

The issue remained is error on

call_user_func('foo', $o);

$o could be actual value like $o="abc". (We also should consider functions return reference which should be able to be updated)

Could this error be fixed/removed? If not, I'll just document call_user_func() parameter cannot be a reference for any type of variables.
 [2021-09-22 11:43 UTC] cmb@php.net
-Assigned To: +Assigned To: cmb
 [2021-09-22 11:43 UTC] cmb@php.net
> The issue remained is error on
> 
> call_user_func('foo', $o);

Just don't use call_user_func; just call the callable directly
instead[1], what is possible for ages.  It may be useful to
document that on the call_user_func() page.

The limitation regarding pass by reference is already documented.

[1] <https://3v4l.org/NHHQR>
 [2021-09-22 11:45 UTC] cmb@php.net
-Summary: Function called from call_user_func() emits additional errors with newer PHPs +Summary: call_user_func() doesn't pass-by-ref; variable functions do
 [2021-09-22 11:57 UTC] git@php.net
Automatic comment on behalf of cmb69
Revision: https://github.com/php/doc-en/commit/2eb43ad4f41a8c62516abd804721786e50717af6
Log: Fix #73238: call_user_func() doesn't pass-by-ref; variable functions do
 [2021-09-22 11:57 UTC] git@php.net
-Status: Assigned +Status: Closed
 [2021-09-22 14:38 UTC] git@php.net
Automatic comment on behalf of mumumu
Revision: https://github.com/php/doc-ja/commit/202dded56cbfcbe7fa09dc1abd100cc96312b141
Log: Fix #73238: call_user_func() doesn't pass-by-ref; variable functions do
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 17:01:30 2024 UTC