|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76206 Arrays in objects using splat aren't passed by reference in a dynamic call
Submitted: 2018-04-10 18:10 UTC Modified: 2019-08-21 13:22 UTC
From: d28b312d at opayq dot com Assigned:
Status: Verified Package: Scripting Engine problem
PHP Version: Irrelevant OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
Solve the problem:
50 - 1 = ?
Subscribe to this entry?

 [2018-04-10 18:10 UTC] d28b312d at opayq dot com
If you have an object with an array parameter, and you try to pass that to a closure which accepts parameters by reference, the parameters aren't taken in by reference and instead by value.

Modifying the below script to use `$foo` instead of `$this->foo` in both cases will work, but using the instance assigned to an object parameter seems to pass the values by value without an error, instead of by reference like the function implies.

If you try to use `call_user_func_array` it throws a message saying it's passing by value to a reference expecting function which is at least better than showing nothing and just treating as if it's a value (but both are wrong!).
`PHP Warning:  Parameter 1 to {closure}() expected to be a reference, value given`

Test script:

class Foo
    private $foo;

    public function call(Closure $cl, $foo)
        $this->foo = $foo;
        return $cl(...$this->foo);

$f = new Foo;
echo $f->call(function(&$i){return ++$i;}, [0]);

Expected result:
array(1) {
  [0] =>

Actual result:
array(1) {
  [0] =>


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2019-08-21 13:22 UTC]
I tried to fix this in, but had to revert due to bug #78356.
 [2019-08-21 13:22 UTC]
-Status: Open +Status: Verified -Package: Unknown/Other Function +Package: Scripting Engine problem
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Fri Mar 24 17:04:43 2023 UTC