1
为什么,如果我有对象的数组是这样的:PHP的foreach对对象的数组
class testClass {
private $_x = 10;
public function setX($x) {
$this->_x = $x;
}
public function writeX() {
echo $this->_x . '<br />';
}
}
$t = array();
for ($i = 0; $i < 10; $i++) {
$t[] = new testClass();
}
print_r($t);
我可以的foreach这样的循环:
foreach ($t as $tt) {
$tt->y = 7;
$tt->setX($counter);
$counter+=100;
}
print_r($t);
或者这样:
foreach ($t as &$tt) {
$tt->y = 7;
$tt->setX($counter);
$counter+=100;
}
print_r($t);
和结果会是平等的吗?但是,如果我有数组中的标量值,它们只能通过($ arr作为& $ v),仅通过引用$ v进行修改?
所以我必须记住这种行为“原样”? – 2011-05-20 13:14:23
或者,您可能需要阅读以下内容:http://www.php.net/manual/en/language.oop5.references.php – 2011-05-20 13:24:48
哪里可以在本手册中找到有关我的问题的信息? – 2011-05-20 13:41:30