note 19698 modified in function.call-user-func by danbrown

From: Date: Sat, 19 Jul 2008 21:15:15 +0000
Subject: note 19698 modified in function.call-user-func by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-141749@lists.php.net to get a copy of this message
The previous note I posted had an error in the source code. That has been corrected in this note. Note, that returning by reference does not work properly when the function is called using call_user_func. This example illustrates the problem... <?php $globalVar = 0; function &staticFunction() { global $globalVar; return $globalVar; } $result =& call_user_func( "staticFunction" ); $result = 3; print "result:" . $result . "<br/>\n"; print "globalVar: " . $globalVar . "<br/>\n"; $result2 =& staticFunction(); $result2 = 3; print "result2: " . $result2 . "<br/>\n"; print "globalVar: " . $globalVar . "<br/>\n"; ?> The above code results in the following output ... Note that $result is not a reference to $globalVar. result:0 globalVar: 0 result2: 3 globalVar: 3 Also, the use of call_user_method is now deprecated in favor of passing array( &$object, $method ) as the function to call_user_func. This is reported when error reporting is set to E_ALL in the latest versions of PHP. --was-- The previous note I posted had an error in the source code. That has been corrected in this note. Note, that returning by reference does not work properly when the function is called using call_user_func. This example illustrates the problem... $globalVar = 0; function &staticFunction() { global $globalVar; return $globalVar; } $result =& call_user_func( "staticFunction" ); $result = 3; print "result:" . $result . "<br/>\n"; print "globalVar: " . $globalVar . "<br/>\n"; $result2 =& staticFunction(); $result2 = 3; print "result2: " . $result2 . "<br/>\n"; print "globalVar: " . $globalVar . "<br/>\n"; The above code results in the following output ... Note that $result is not a reference to $globalVar. result:0 globalVar: 0 result2: 3 globalVar: 3 Also, the use of call_user_method is now deprecated in favor of passing array( &$object, $method ) as the function to call_user_func. This is reported when error reporting is set to E_ALL in the latest versions of PHP. http://php.net/manual/en/function.call-user-func.php

« previous php.notes (#141749) next »