【发布时间】: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,&$reply)