Php 为什么我的变量会发生这种情况?

Php 为什么我的变量会发生这种情况?,php,Php,看一看:这是预期的行为 从PHP5开始,对象变量不再包含对象本身作为值。它只包含一个允许对象访问器查找实际对象的对象标识符当对象通过参数发送、返回或分配给另一个变量时,不同的变量不是别名:它们持有指向同一对象的标识符副本 如果你想避免这种情况,你应该在必要的地方。就是这样, <?php // $searchResult is type of Outcome // This is what we do: dList::lessAnchor($searchResult)->showEl

看一看:这是预期的行为

从PHP5开始,对象变量不再包含对象本身作为值。它只包含一个允许对象访问器查找实际对象的对象标识符当对象通过参数发送、返回或分配给另一个变量时,不同的变量不是别名:它们持有指向同一对象的标识符副本

如果你想避免这种情况,你应该在必要的地方。就是这样,

<?php

// $searchResult is type of Outcome
// This is what we do:
dList::lessAnchor($searchResult)->showElement();
dList::moreAnchor($searchResult)->showElement();

/**
* @returns vAnchor (can get showed with showElement() - not part of the problem)
*/
public static function lessAnchor(Outcome $searchResult){
  $searchData = $searchResult->searchData;
  $searchData->Page = $searchData->Page - 1; // (!1)
  return self::basicAnchor($searchData, "Back");
}

/**
* @returns vAnchor (can get showed with showElement() - not part of the problem)
*/
public static function moreAnchor(Outcome $searchResult){
  $searchData=$searchResult->searchData;
  $searchData->Page = $searchData->Page + 1; // (!2)
  return self::basicAnchor($searchData, "More");
}

哇,我没想到会这样。我应该在函数调用时克隆对象吗?-好的,我知道了。谢谢你的建议和质量
public static function lessAnchor(Outcome $searchResult){
  $searchData = clone $newResult->searchData; //$searchData now is a new object
  $searchData->Page=$searchData->Page-1; // (!1)
  return self::basicAnchor($searchData,"Back");
}