在此代碼:PHP中的對象是通過值還是引用傳遞的?
<?php
class Foo
{
var $value;
function foo($value)
{
$this->setValue($value);
}
function setValue($value)
{
$this->value=$value;
}
}
class Bar
{
var $foos=array();
function Bar()
{
for ($x=1; $x<=10; $x++)
{
$this->foos[$x]=new Foo("Foo # $x");
}
}
function getFoo($index)
{
return $this->foos[$index];
}
function test()
{
$testFoo=$this->getFoo(5);
$testFoo->setValue("My value has now changed");
}
}
?>
當該方法Bar::test()
運行和它FOO的對象的陣列中的改變的FOO#5的值,將所述陣列中的實際FOO#5受到影響,或者將所述$testFoo
變量只能是一個局部變量,它將在函數結尾處不再存在?
相關問題:http://stackoverflow.com/questions/879/php-variables-passed-by-value-or-by-reference。 – TSomKes 2009-07-09 23:54:43