PHP函数返回数组

时间:2014-05-22 16:45:16

标签: php arrays return

PHP array documentation表示Array assignment always involves value copying。当函数返回一个数组时,这是真的吗?

具体来说,我想返回私有数组的副本,以便调用代码对返回的数组所做的修改不会影响类的私有数组:

class Insecurity {

  private $everyonesPasswordsInPlainText = ["abcd"];

  public function showPassesToHacker() {
    return $this->everyonesPasswordsInPlainText;
  }

}

// shouldn't affect Insecurity->$everyonesPasswordsInPlaineText
$badSite = new Insecurity();
$passes = $badSite->showPassesToHacker();
array_push($passes, "1234");

$origPasses = $badSite->showPassesToHacker();

print_r($passes);     // ["abcd", "1234"]
print_r($origPasses); // ["abcd"]

0 个答案:

没有答案