|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2019-08-21 13:22 UTC] nikic@php.net
[2019-08-21 13:22 UTC] nikic@php.net
-Status: Open
+Status: Verified
-Package: Unknown/Other Function
+Package: Scripting Engine problem
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 19:00:01 2025 UTC |
Description: ------------ 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. Note: 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: --------------- <?php class Foo { private $foo; public function call(Closure $cl, $foo) { $this->foo = $foo; $cl(...$this->foo); var_dump($this->foo); return $cl(...$this->foo); } } $f = new Foo; echo $f->call(function(&$i){return ++$i;}, [0]); Expected result: ---------------- /tmp/classtest.php:11: array(1) { [0] => int(1) } 2 Actual result: -------------- /tmp/classtest.php:11: array(1) { [0] => int(0) } 1