PHP ist nicht meine Stärke, aber ich habe heute einige OO-Code versucht. Alles gut, außer dass mein Array $ replyArray auf globaler Ebene nicht von der Klassenmethode jumble() meines solSet-Objekts geschrieben wird. Der vorletzte var_dump in meinem Code zeigt ein leeres Array. Ich habe versucht, globale Keywords zu umwerfen, und das hat nicht geholfen. Weil ich diese Variable explizit mit Bezug auf meine neu instanziierte Klasse übergebe, sollte das nicht genug sein? Danke! kkKann keine Werte in ein globales PHP-Array von einer Klassenmethode schreiben
<?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);
?>
Dies scheint zu beziehen: Using a global array inside a class Aber das ist, was ich getan habe. Auch die ganze Welt und sein Hund sagen, dass das globale Schlüsselwort "es falsch macht". Was ist zu tun?
Sie sind nicht wirklich * "explizit übergeben diese Variable durch Verweis" * - das wäre 'öffentliche Funktion jumble ($ wh, $ ts, $ arrShuf, & $ antworten)' – CD001