如何传递对象构造函数的引用,并允许该对象更新该引用?
class A{ private $data; function __construct(&$d){ $this->data = $d; } function addData(){ $this->data["extra"]="stuff"; } } // Somewhere else $arr = array("seed"=>"data"); $obj = new A($arr); $obj->addData(); // I want $arr to contain ["seed"=>"data","extra"=>"stuff"] // Instead it only contains ["seed"=>"data"]
您必须将它存储在任何地方作为参考.
function __construct (&$d) { $this->data = &$d; // the & here }