[請益] 沒用 = & 卻有 reference 的效果?
<php
class Item {
var $name;
function __construct($name) { $this->name = $name; }
}
class Box {
var $items;
function __construct() {
$this->items = array();
}
function replace() {
$_items = $this->items;
print_r($this->items);
$_items[0]->name = 'A';
print_r($this->items);
/*
明明異動的是 $_items 裡的值,
但是 $this->items 也同步異動了。
是否有高人可以指點迷津?謝謝。
*/
}
}
$BBB = new Box();
$BBB->items[] = new Item('a');
$BBB->items[] = new Item('b');
$BBB->items[] = new Item('c');
$BBB->replace();
?>
會得到以下的結果:
Array
(
[0] => Item Object
(
[name] => a
)
[1] => Item Object
(
[name] => b
)
[2] => Item Object
(
[name] => c
)
)
Array
(
[0] => Item Object
(
[name] => A
)
[1] => Item Object
(
[name] => b
)
[2] => Item Object
(
[name] => c
)
)
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.120.49.163
推
12/23 22:16, , 1F
12/23 22:16, 1F
→
12/23 22:18, , 2F
12/23 22:18, 2F
→
12/23 22:19, , 3F
12/23 22:19, 3F
→
12/23 22:22, , 4F
12/23 22:22, 4F
→
12/23 22:35, , 5F
12/23 22:35, 5F
→
12/24 20:37, , 6F
12/24 20:37, 6F
推
12/25 12:22, , 7F
12/25 12:22, 7F
→
12/28 03:20, , 8F
12/28 03:20, 8F
→
12/28 03:23, , 9F
12/28 03:23, 9F
→
12/28 03:23, , 10F
12/28 03:23, 10F
→
12/28 03:23, , 11F
12/28 03:23, 11F
→
12/29 11:59, , 12F
12/29 11:59, 12F