このコードを試してみると:
<?php
class ref
{
public $reff = "original ";
public function &get_reff()
{
return $this->reff;
}
public function get_reff2()
{
return $this->reff;
}
}
$thereffc = new ref;
$aa =& $thereffc->get_reff();
echo $aa;
$aa = " the changed value ";
echo $thereffc->get_reff(); // says "the changed value "
echo $thereffc->reff; // same thing
?>
次に、参照による戻りが機能し、それを参照$reff
する変数も変更されると、オブジェクト プロパティの値が変更され$aa
ます。
しかし、クラス内ではない通常の関数でこれを試してみると、うまくいきません!!
私はこのコードを試しました:
<?php
function &foo()
{
$param = " the first <br>";
return $param;
}
$a = & foo();
$a = " the second <br>";
echo foo(); // just says "the first" !!!
foo()
関数が参照によって返されることを認識せず、頑固に必要なものを返すように見えます!!!
参照による戻りは、OOP コンテキストでのみ機能しますか??