PHP编辑变量值编辑其他变量值
我对PHP中的这种行为非常困惑,我不知道如何解决它。 我想这样做: 我生成PHP编辑变量值编辑其他变量值,php,laravel,variables,laravel-5,Php,Laravel,Variables,Laravel 5,我对PHP中的这种行为非常困惑,我不知道如何解决它。 我想这样做: 我生成对象,并使用一些方法设置其属性。然后我想“缓存”对象,所以我会将它存储到其他变量中,然后我对该对象执行其他操作,但它也会影响缓存的对象。 你能给我一些建议,怎么做 下面是代码片段: $query = new Obj(); $this->item->generateItemsQuery($query); $this->itemsQuery = $query; // here I "cache" the va
对象
,并使用一些方法设置其属性。然后我想“缓存”对象,所以我会将它存储到其他变量中,然后我对该对象执行其他操作,但它也会影响缓存的对象。
你能给我一些建议,怎么做
下面是代码片段:
$query = new Obj();
$this->item->generateItemsQuery($query);
$this->itemsQuery = $query; // here I "cache" the variable for next usage...
// here I edit the old variable $query
if ($this->getFilter('limit') !== null) {
$query = $query->limit($this->getFilter('limit'));
}
if ($this->getFilter('page') !== null) {
$offset = ($this->getFilter('page') - 1) * $this->getFilter('limit');
$query = $query->offset($offset);
}
public function generateItemsQuery(&$query)
{
// some other things like this: $query = $query->offset($offset);
}
在本例中->问题是,当我对$query应用方法“limit”和“offset”时,它也会影响$this->itemsQuery
你能给我一些解决办法吗
非常感谢您可能想看看: 具体而言: 对象变量不再包含对象本身作为值。它只包含一个允许对象访问器查找实际对象的对象标识符 如果要创建对象的克隆,需要执行以下操作:
$this->itemsQuery=clone$query代码>
请参阅:完全正确!非常感谢@craig_h@JanKo那么,请考虑将克雷格的答案设为“接受”: