__callStatic(), call_user_func_array(), references, and PHP 5.3.1

Argoman

New Member
I've been reading around on SO, and elsewhere, however I can't seem to find anything conclusive.Is there any way to effectively carry references through this call stack, resulting in the desired functionality as described in the example below? While the example doesn't try to solve it, it certainly illustrates the problem:\[code\]class TestClass{ // surely __call would result similarly public static function __callStatic($function, $arguments){ return call_user_func_array($function, $arguments); }}// note argument by referencefunction testFunction(&$arg){ $arg .= 'bar';}$test = 'foo';TestClass::testFunction($test);// expecting: 'helloworld'// getting: 'hello' and a warning about the referenceecho $test;\[/code\]For the sake of inspiring a potential resolution, I'm going to add summary details here:Focusing on only \[code\]call_user_func_array()\[/code\], we can determine that (at least with PHP 5.3.1) you cannot implicitly pass arguments by reference:\[code\]function testFunction(&$arg){ $arg .= 'bar';}$test = 'foo';call_user_func_array('testFunction', array($test));var_dump($test);// string(3) "foo" and a warning about the non-reference parameter\[/code\]By explicitly passing the array element \[code\]$test\[/code\] as a reference, we can alleviate this:\[code\]call_user_func_array('testFunction', array(&$test));var_dump($test);// string(6) "foobar"\[/code\]When we introduce the class with \[code\]__callStatic()\[/code\], an explicit call-time parameter by reference seems to carry through as I expected, but deprecation warnings are issued (in my IDE):\[code\]class TestClass{ public static function __callStatic($function, $arguments){ return call_user_func_array($function, $arguments); }}function testFunction(&$arg){ $arg .= 'bar';}$test = 'foo';TestClass::testFunction(&$test);var_dump($test);// string(6) "foobar"\[/code\]Omission of the reference operator in \[code\]TestClass::testFunction()\[/code\] results in \[code\]$test\[/code\] being passed by value to \[code\]__callStatic()\[/code\], and of course is passed by value as an array element to \[code\]testFunction()\[/code\] via \[code\]call_user_func_array()\[/code\]. This results in a warning, since \[code\]testFunction()\[/code\] expects a reference.Hacking around, some additional details have surfaced. The \[code\]__callStatic()\[/code\] definition, if written to return by reference (\[code\]public static function &__callStatic()\[/code\]) has no visible effect. Furthermore, recasting the elements of the \[code\]$arguments\[/code\] array in \[code\]__callStatic()\[/code\] as references we can see that \[code\]call_user_func_array()\[/code\] works somewhat as expected:\[code\]class TestClass{ public static function __callStatic($function, $arguments){ foreach($arguments as &$arg){} call_user_func_array($function, $arguments); var_dump($arguments); // array(1) { // [0]=> // &string(6) "foobar" // } }}function testFunction(&$arg){ $arg .= 'bar';}$test = 'foo';TestClass::testFunction($test);var_dump($test);// string(3) "foo"\[/code\]These results are expected, as \[code\]$test\[/code\] is no longer passed by reference, the change is not passed back into it's scope. However, this confirms that \[code\]call_user_func_array()\[/code\] is in fact working as expected, and that the issue is certainly confined to the calling magic.Upon further reading, it appears it may be a "bug" in PHP's handling of user functions, and the \[code\]__call()\[/code\]/\[code\]__callStatic()\[/code\] magic. I've perused the bug database for existing or related issues, and had found one, but haven't been able to locate it again. I'm considering issuing another report, or a request for the existing report to be reopened.
 
Top