如何将引用传递给对象构造函数,并允许该对象更新该引用?
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
}
您必须告诉PHP也为私有成员data
分配一个引用,如下所示:
$this->data = &$d;
根据上下文的不同,您可能不想使用对外部数组的引用,最好将该数组放在处理它的对象中
请注意,构造函数被称为__construct
,而不是__construction
。
这将满足您的要求:
class Test {
private $storage;
public function __construct(array &$storage)
{
$this->storage = &$storage;
}
public function fn()
{
$this->storage[0] *= 10;
}
}
$storage = [1];
$a = new Test($storage);
$b = new Test($storage);
$a->fn();
print_r($a); // $storage[0] is 10
print_r($b); // $storage[0] is 10
$b->fn();
print_r($a); // $storage[0] is 100
print_r($b); // $storage[0] is 100
备选方案1
您也可以使用ArrayObject
、ArrayIterator
或SplFixedArray
,而不是使用数组。由于这些都是对象,它们将通过引用传递。所有这些都实现了ArrayAccess
,因此您可以通过方括号访问它们,例如
$arrayObject = new ArrayObject;
$arrayObject['foo'] = 'bar';
echo $arrayObject['foo']; // prints 'bar'
备选方案2
不要使用泛型类型,而是使用专用类型。找出您在该数组中存储的内容。它是Config
吗?Registry
?UnitOfWork
?找出它到底是什么,然后把它变成一个对象,并给它一个反映责任的API。然后注入那个对象并通过那个API访问它。
参见Martin Fowler的这篇论文,以获得关于何时制作类型的一些指导