参照関数
$name = 'alfa';
$address = 'street';
//declaring the function with the $ tells PHP that the function will
//return the reference to the value, and not the value itself
function &function_name($what){
//we need to access some previous declared variables
GLOBAL $name,$address;//or at function declaration (use) keyword
if ($what == 'name')
return $name;
else
return $address;
}
//now we "link" the $search variable and the $name one with the same value
$search =& function_name('name');
//we can use the result as value, not as reference too
$other_search = function_name('name');
//any change on this reference will affect the "$name" too
$search = 'new_name';
var_dump($search,$name,$other_search);
//will output string 'new_name' (length=8)string 'new_name' (length=8)string 'alfa' (length=4)
通常、同じインターフェースを実装したオブジェクトでメソッドを使用し、次に操作するオブジェクトを選択します。
参照による通過:
function ($var, &$var2, $var3){...}
例を見たと思いますので、いつどのように使うかを説明します。基本的なシナリオは、現在のオブジェクト/データに適用したい大きなロジックがあり、それを(メモリ内に)それ以上コピーしたくない場合です。お役に立てれば。