【发布时间】:2014-09-08 08:37:36
【问题描述】:
我最近一直在处理较旧的项目并对其进行了一些升级。我已经构建了类似于下面的代码:
class Foo{
public $some_variable = "AAA";
public function do_some_action(){
echo $this->some_variable;
// Prints out 'AAA'
$bar = new Bar($this);
$bar->different_action();
echo $this->some_variable;
// Prints out 'BBB' - why ?
}
} class Bar {
//Constructor
public function Bar($foo){
$this->foo = $foo;
}
public function different_action(){
$this->foo->some_variable = "BBB";
}
}
我不完全明白为什么函数 Bar::different_action() 会影响 Foo 的公共变量。 >>$this
public function Bar(&$foo){
$this->foo =& $foo;
}
我的逻辑肯定失败了,但如果有人告诉我在哪里以及为什么,我将不胜感激:)
【问题讨论】:
-
对象通过引用传递。
-
谢谢 - 这可能看起来很简单,但我不知道!
-
接受我的回答将是您的好心
-
我会在 8 分钟内完成。再次感谢!
标签: php object pass-by-reference