我最近一直在处理较旧的项目,并对它们进行了一些升级。我已经构建了类似于以下代码的代码:
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;
}
我的逻辑肯定会失败,但是如果有人告诉我在哪里以及为什么,我将不胜感激:)
对象通过引用传递:文档:http : //php.net/manual/en/language.oop5.references.php
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句