# 框架中的PHP對象引用

$foo = 'Hello World';$bar = &$foo; echo (is_reference($bar) ? 'Is reference' : 'Is orginal';


class Foo{
public $var = 'Hello World!'; } class Bar{ public function __construct(){ global$Foo;
echo $Foo->var; } }$Foo = new Foo;
$Bar = new Bar;  我很驚訝地看到CodeIgnitor和Yii傳遞了對象的參考，可以通過以下方法訪問： $this->load->view('argument')


$Baz =$Foo;
$Baz->var = 'Goodbye World!'; echo$Foo->var;

/* Ouput */
Goodbye World!


## 最佳答案

The & operator is the PHP4 way to pass objects by reference, PHP5 does this by default. Code that still uses this operator is ment to be backward compatible.

You can still use & to pass other datatypes by reference in PHP5, but the need for it is very rare and i suggest to avoid it.