工作如果我試試這個代碼:PHP返回參照不正常功能工作,但與OOP
<?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環境中通過引用工作返回?
我真正感興趣的,爲什麼你想那個呢?什麼曾經迫使你做這樣的事情? –
madara我讀了關於通過引用返回和通過我閱讀wordpress中使用的函數的方式,所以我想知道什麼編碼器希望通過在他的功能的名稱之前添加一個&! – hanachi
在返回一些大的情況下(不只是一個字符串,而是說一個巨大的結果集)如果通過引用返回,你通過它的地址返回,而不是複製整個結果到一個新的變量。它並不像你所假設的那樣將函數範圍內的變量保留下來。 – Doon