如果我通过引用将一个变量或对象传递给一个函数调用,我会想象它会避免在内存中创建第二个对象,从而保留资源?例如:是否通过引用传递避免创建新变量?
<?php
class CoolObject
{
// Have some properties here
public function __construct()
{
// Do some stuff
}
public function test()
{
echo("Test");
}
}
function doSomething(&$param)
{
// Calls original instance, still only one object in memory
$param->test();
// Does this create a second instance in memory, or just assign
// the reference?
$newObj = $param;
}
// Create 1st instance of object in memory
$myObj = new CoolObject;
// Do a test to determine number of instances created
doSomething($myObj);
?>
当我分配了“参照”变量$ newObj,并在内存中创建一个新的使数最多两个,还是它只是通过仍然只留下一个对象的引用?
好的,我明白了。那么第二部分呢......是$ newObj一个新实例还是$ myObj的“指针”? – 2012-02-02 22:21:06