【问题标题】:Can't write values into a global PHP array from a class method无法从类方法将值写入全局 PHP 数组
【发布时间】:2016-07-21 10:46:26
【问题描述】:

PHP 不是我的强项,但我今天尝试了一些 OO 代码。除了我在全局级别的 $replyArray 数组没有被我的 solSet 对象的 jumble() 类方法写入之外,一切都很好。我的代码中倒数第二个 var_dump 显示了一个空数组。我尝试过使用全局关键字,但没有帮助。因为我通过引用我新实例化的类来显式传递这个变量,这还不够吗? 谢谢! kk

<?php


//create a solution set for translation of incoming user login requests

$widhi = 600;
$tileDim = 25;
$randArray = array ("0","1","2","3","4","5");
$replyArray = array ();


//create 5 positions and ensure neither overlap or edge collision
class solSet
{
  var $pos1;
  var $pos2;
  var $pos3;
  var $pos4;
  var $pos5;
  var $pos6;


  public function jumble($wh,$ts,$arrShuf,$reply)
  {
    foreach($this as $key => $value)
    {
      $newX = rand (($ts/2),$wh - ($ts/2));
      $newY = rand (($ts/2),$wh - ($ts/2));
      $randNo = array_pop($arrShuf);
      $value = "" . $newX . "_" . $newY . "_" . $randNo;
      $this->$key = $value;
      //push coords onto an array for later ajax
      $pushElem = "" . $newX . "_" . $newY;
      $reply[] = $pushElem;
    }
  }

}


//scramble the random number array for later popping
shuffle($randArray);

//make a solution object
$aSolSet = new solSet;
$aSolSet->jumble($widhi,$tileDim,$randArray,$replyArray);

//store it in a session
session_start();
$_SESSION["solSet"] = $aSolSet;

echo var_dump($replyArray);
echo json_encode($aSolSet);

?>

这似乎与: Using a global array inside a class 但这就是我所做的。整个世界和他的狗都在说全球关键词是“做错了”。怎么办?

【问题讨论】:

  • 你实际上并没有“通过引用显式传递这个变量” - 那将是public function jumble($wh,$ts,$arrShuf,&amp;$reply)

标签: php arrays object


【解决方案1】:

您的jumble 方法需要通过引用获取$replyArray - 默认情况下,PHP 函数按值操作,这意味着它们对变量的副本进行操作,而不是修改它。见http://php.net/manual/en/language.references.pass.php

改变

public function jumble($wh,$ts,$arrShuf,$reply)

public function jumble($wh,$ts,$arrShuf,&$reply)

变量名前面的和号表示参数是通过引用传递的。

【讨论】:

  • 勾选,因为这正是问题所在。我假设参考。谢谢!
【解决方案2】:

或者,您可以简单地返回新的随机数组,而不是从类中更新全局变量(不利于重用和可移植性):

public function jumble($wh,$ts,$arrShuf)
{
  $reply = array();
  foreach($this as $key => $value)
    {
      $newX = rand (($ts/2),$wh - ($ts/2));
      $newY = rand (($ts/2),$wh - ($ts/2));
      $randNo = array_pop($arrShuf);
      $value = "" . $newX . "_" . $newY . "_" . $randNo;
      $this->$key = $value;

      //push coords onto an array for later ajax
      $pushElem = "" . $newX . "_" . $newY;
      $reply[] = $pushElem;
    }

  return $reply;
}

...并使用响应更新您的全局$replyArray

//make a solution object
$aSolSet = new solSet;
$replyArray = $aSolSet->jumble($widhi,$tileDim,$randArray);

您甚至根本不需要将$reply 参数传递到您的方法中(注意,少了一个参数),一切都很好且独立。

【讨论】:

  • 谢谢,这正是我要做的。我已经勾选了下面的答案,因为这是我的代码为什么不能工作而不是重写的答案。不过,你的回答对我来说是最有意义的。我只是固执,这更符合逻辑。
  • 很公平 - 我几乎在同一时间发表了与 iaiann 相同的评论。我也赞成他的回答,因为它完全符合您的要求......不过,就个人而言,我更喜欢这种方法而不是通过引用传递;)
【解决方案3】:

在函数中引用全局数组

 global $replyArray;

【讨论】:

  • 感谢您的回复。有人告诉我这很顽皮!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-17
  • 2012-03-06
  • 2013-06-30
  • 2022-10-14
  • 2016-10-16
  • 1970-01-01
  • 2016-06-02
相关资源
最近更新 更多