#24931 [Opn->Csd]: call_user_func ignores requirement of references

From: Date: Fri, 06 Aug 2004 22:18:01 +0000
Subject: #24931 [Opn->Csd]: call_user_func ignores requirement of references
References: 1  Groups: php.doc 
Request: Send a blank email to phpdoc+get-969362793@lists.php.net to get a copy of this message
ID: 24931 Updated by: vrana@php.net Reported By: rehsack at liwing dot de -Status: Open +Status: Closed Bug Type: Documentation problem Operating System: FreeBSD 5.1 i386 PHP Version: 4.3.3RC2, 5.0.0b2-dev New Comment: This bug has been fixed in the documentation's XML sources. Since the online and downloadable versions of the documentation need some time to get updated, we would like to ask you to be a bit patient. Thank you for the report, and for helping us make our documentation better. "Note that the parameters for call_user_func are not passed by reference." + example: <?php function increment(&$var) { $var++; } $a = 0; call_user_func('increment', $a); echo $a; // 0 call_user_func_array('increment', array(&$a)); // You can use this instead echo $a; // 1 ?> Previous Comments: ------------------------------------------------------------------------ [2003-08-05 02:29:54] rehsack at liwing dot de Documentation says: call_user_func (PHP 3>= 3.0.3, PHP 4 ) call_user_func -- Call a user function given by the first parameter Description mixed call_user_func ( callback function [, mixed parameter [, mixed ...]]) Call a user defined function given by the function parameter. Take the following: ... I consider update the documentation by directly writing: "This method doesn't take care about using parameters as references as long as you specify the at calltime, which should be enabled via php.ini even if it's deprecated, because there's no other way to call this method using references.<br /> We consider to use call_user_func_array( func_name, array( &$param ) ) if you want to use references." ------------------------------------------------------------------------ [2003-08-04 19:34:57] iliaa@php.net Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php I admit that the error is misleading, because it cannot tell that function was not executed directly but rather via call_user_func(). However, the actual functionality of the code is correct. The variable is not passed by reference to call_user_func(), hence making it pass a copy to nextID(). The documentation does not mention that you can pass variables by reference and that's because in this case you cannot. ------------------------------------------------------------------------ [2003-08-04 17:06:16] rehsack at liwing dot de Hi Ilia, your wrong, because: $ cat test1.php <?PHP function nextId( &$id ) { $id += 1; } $id = 1; echo "before call_user_func id=$id\n"; call_user_func( "nextId", &$id ); echo "after call_user_func id=$id\n"; call_user_func_array( "nextId", array( &$id ) ); echo "after call_user_func_array id=$id\n"; ?> $ php test1.php Warning: Call-time pass-by-reference has been deprecated - argument passed by value; If you would like to pass it by reference, modify the declaration of call_ user_func(). If you would like to enable call-time pass-by-reference, you can set allow_call_time_pass_reference to true in your INI file. However, future versions may not support this any longer. in /var/www/data/trevor/flexpage/test/php-bugs/test1.php on line 10 before call_user_func id=1 after call_user_func id=2 after call_user_func_array id=3 This gives the wanted result but prints out a warning. By the way, I don't want to discus whether call_user_func is implemented as described neither it's not working as designed. I want to tell you (as php team), that the design might be wrong. Either, if you (as php team) don't have any plans to extend the design (what shouldn't be to complicated), I consider you update the documentation that way, that you describe that call_user_func will duplicate the given argument. call_user_func is not an intelligent function (what it could be), it's a dumb one (this is not an offending but it is like a dumb terminal as some old unix ones). I want to consider by this bug report to rewrite call_user_func becomes intelligent (looks what kind of argument is required and calls that way). Otherwise I had to open a documentation bug, because this behaviour should be documented explicitely. ------------------------------------------------------------------------ [2003-08-04 13:37:42] iliaa@php.net Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php $id is modified because the original $id passed to call_user_func() is not being passed by reference. ------------------------------------------------------------------------ [2003-08-04 03:22:31] rehsack at liwing dot de Description: ------------ The method call_user_func ignores requirement of called function takes it's arguments as reference. The results in several times very ugly and hard to find bugs, eg. if you use a template pattern combined with composite to control child templates and want to do an action in each child object and require to work on current data copy. Either, if call_user_func internally is able to be called by reference, some programs could be speed up, too (because of the loose of expensive string copies) This bug may related to #24631 but seems to have another background. I can submit a more complicated example using templates and a composite object to illustrate the problem. Enabling call-time references in php.ini would solve this problem by don't write a warning but I don't think it's a good way to solve... Reproduce code: --------------- <?PHP function nextId( &$id ) { $id += 1; } $id = 1; echo "before call_user_func id=$id\n"; call_user_func( "nextId", $id ); echo "after call_user_func id=$id\n"; call_user_func_array( "nextId", array( &$id ) ); echo "after call_user_func_array id=$id\n"; ?> Expected result: ---------------- before call_user_func id=1 after call_user_func id=2 after call_user_func_array id=3 Actual result: -------------- before call_user_func id=1 after call_user_func id=1 after call_user_func_array id=2 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=24931&edit=1

« previous php.doc (#969362793) next »