PHP类不存储引用
发布时间:2020-12-13 22:42:14 所属栏目:PHP教程 来源:网络整理
导读:如何传递对象构造函数的引用,并允许该对象更新该引用? 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);$ob
如何传递对象构造函数的引用,并允许该对象更新该引用?
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 } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |