如何传递对象构造函数的引用,并允许该对象更新该引用?
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"]
您必须将它存储在任何地方作为参考.
原文链接:https://www.f2er.com/php/133369.htmlfunction __construct (&$d) { $this->data = &$d; // the & here }