note 99549 modified in language.references.pass by danbrown
| From: | danbrown@php.net | Date: | Tue, 24 Aug 2010 12:51:46 +0000 |
| Subject: | note 99549 modified in language.references.pass by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-171973@lists.php.net to get a copy of this message | ||
Someone mentioned that passing reference doesn't work as expected from within
call_user_func(). For example:
<?php
$string = 'string';
function change(&$str) {
$str = 'str';
}
call_user_func('change', $string);
echo $string;
?>
output:
string //not as expected 'str'
Here we could assume call_user_func as below
<?php
function call_user_funct($func, $param) {
$func($param);
}
?>
As calling $func() inside call_user_funct, here is change(), the variable $param is copied to a
third variable then the temp variable is passed to the $func. So $str only hold the reference of the
temp variable inside change().
Of course , if we pass &$string directly to call_user_func we could always get the result as
expected (str).
--was--
Someone mentioned that passing reference doesn't work as expected from within
call_user_func(). For example:
$string = 'string';
function change(&$str) {
$str = 'str';
}
call_user_func('change', $string);
echo $string;
output:
string //not as expected 'str'
Here we could assume call_user_func as below
function call_user_funct($func, $param) {
$func($param);
}
As calling $func() inside call_user_funct, here is change(), the variable $param is copied to a
third variable then the temp variable is passed to the $func. So $str only hold the reference of the
temp variable inside change().
Of course , if we pass &$string directly to call_user_func we could always get the result as
expected (str).
http://php.net/manual/en/language.references.pass.php