如何使不同索引的 PHP 数组指向相同的对象
在一个类中,我有三个数组(作为实例变量),它们必须全部指向相同的对象。第一个是我从某些外部函数返回的数字索引数组。第二个数组必须按名称索引相同的对象。第三个是其他财产。这是为了通过这些属性快速访问。所以重点是:我使用哪个数组来访问对象并修改它并不重要。
但它无法实现这一点。我了解 PHP 参考资料。我知道java参考资料。我了解 C 指针,但我无法实现它 - 无论我尝试在何处放置与号 (=&)。
class xxx {
private $objs;
private $objsByName;
public function __construct() {
$this->objs = getObjs();
$this->objsByName = array();
foreach($this->objs as $obj) {
$this->objsByName[$obj->getName()] = $obj;
}
}
}
这里没有任何地方我没有尝试过用 =& 替换 = 。
我缺少什么吗?
Within a class I have three arrays (as instance variables) that must all point to the same objects. The first is a numerically indexed array that I get back from some external function. The second array must index the same objects by their name. The third by some other property. This is for fast access via those properties. So the point is: it shouldn't matter which array I use to access an object and modify it.
Yet it can't make it happen. I know about PHP references. I know about java references. I know about C pointers, but I can't make it happen - wherever I try to place the ampersand (=&).
class xxx {
private $objs;
private $objsByName;
public function __construct() {
$this->objs = getObjs();
$this->objsByName = array();
foreach($this->objs as $obj) {
$this->objsByName[$obj->getName()] = $obj;
}
}
}
There is no place here where I have not tried replacing = with =&
I am missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
用这个循环替换你的 foreach 循环。
这将解决您的问题:)
谢谢!
侯赛因。
Replace your foreach loop with this one.
This will solve your problem :)
Thanks!
Hussain.
在 PHP 中,对象始终通过引用传递(除非使用 <代码>克隆关键字)。此代码说明了这一点:
问题可能出在其他地方。
In PHP, objects are always passed by reference (you cannot copy an object unless you use
clone
keyword). This code illustrates this:The issue is probably somewhere else.
从 php 手册中标记这部分:
http://php.net/manual/en/control-structs.foreach.php
所以你的 $obj 是一个副本。
我还没有测试 Hussain 的代码,但这看起来是一个不错的选择:)
Mark this part from the php manual:
http://php.net/manual/en/control-structures.foreach.php
So your $obj is a copy.
I have not tested Hussain's code, but that looks like a good option :)
PHP 总是使用写时复制函数,很可能您的对象在发生更改之前都是相同的。
为了避免错误,您应该说您的数组之一包含数据,而其他数组只是索引。
使用索引查找 id,并使用按 id 索引的数据数组来读取和更改数据。
PHP always use copy on write functions, chances are that your objects will be same until one is changed.
To avoid errors you should say that one of your array contains the data and that thee others are just indexes.
Use the index to find the id, and use tha data array, indexed by id, to read and alter data.
在 PHP5 中,可以将
&
添加到as
变量中,使 foreach 循环不克隆对象:参考:http://php.net/manual/en/control-structs.foreach.php
p/s:我不确定是否需要 foreach 体内的
&
但只需添加它以确保安全。或者您可以尝试将其删除,看看是否需要它。In PHP5 you can add
&
to theas
variable to make the foreach loop not clone the object:Reference: http://php.net/manual/en/control-structures.foreach.php
p/s: I'm not sure if the
&
inside the foreach body is needed or not but just add it to be safe. Or you can try removing it to see if it's needed or not.