I’m trying to have a user-defined list of game-maps. Because I don’t know how many maps will be in the array at design time, I’m trying to dynamically create new variables to contain them. Is this even possible? Here’s my failed attempt:
<?php
$maplist=array("map1.aamap.xml","map2.aamap.xml"); //edit this list with your maps
$rounds = 3; //times to play each map
/*======No need to edit below========*/
global $last; //store the last played map
class Map
{
public $difficulty;
public $played; //amount of times played
}
foreach($maplist as $i => $element)
{
$element = $map[$i];
$map[$i] = new Map();
}
//snipped other code here
$map[$i]->$played = $x++; //increment the times played counter <-- FAILS HERE
?>
Parser says: Fatal error: Cannot access empty property
Is something like this even feasible in this manner?
There are some errors in your code:
Since you are on the global scope here, not inside a function, there is no need for
global.Is some code missing here? You are not using
$elementwithin the loop, so this assignment is not needed.The syntax to access a member variable is
$object->variable, not$object->$variable. The latter one will evaluate$variableand use the value as variable name (E.g., if$variable = "foo", this will try to access$object->foo).Use
$map[$i]->played = $x++;instead.