parino_esquilado Posted December 18, 2010 Share Posted December 18, 2010 I have created two classes. One is called 'move' and the other 'unit'. To make my example easier to follow, I shall use the much loved pokemon series to explain my dilemma. Here are the class declarations. new class unit { var $name, $moves; public function newUnit($name, $moves) { $this->name = $name; $this->moves = $moves; } } new class move { var $name, $attack; public function newMove($name, $attack) { $this->name = $name; $this->moves = $moves; } } Now I shall instantiate these classes (if that makes sense xD) - I just started OOP yesterday, do mind my ignorance. $unit = array(); $move = array(); $unit[0] = new unit; $unit[0]->newUnit("picachu",array("thunderbolt","quick-attack")); Now, let's say I want to populate the $move array with the moves in the unit object (thunderbolt and quick-attack). I can retrieve their attack values from a database: $thunderboltAttack = 200; $quick-attackAttack = 40; I now want to create a function that will create instances of the 'move' class to produce: $move[0] = new move; $move[0]->newMove("thunderbolt",200); $move[1] = new move; $move[1]->newMove("quick-attack",40); Here is my dire attempt to do so: function createUnits($moves){ //$moves is the array holding the unit's moves for($i; $i < count($moves); $i++) { $arr[] =new move; $arr[count($units) - 1]->name = $moves[0]; $arr[count($units) - 1]->attack = $someAttackData; } return $arr; } $moves[] = createUnits; I'm ending up with a multi-dimensional array when I want a linear one like this: $move[0] = new move; $move[0]->newMove("thunderbolt",200); $move[1] = new move; $move[1]->newMove("quick-attack",40); What do I need to do. Do I have to resort to making $move global from within the function and do things that way? Link to comment https://forums.phpfreaks.com/topic/222059-oop-arrays-help-please/ Share on other sites More sharing options...
ignace Posted December 18, 2010 Share Posted December 18, 2010 class Pokemon { protected $type; protected $abilities; protected $gender; protected $weaknesses; protected $evolution; public function __construct(PokemonEvolution $evolution) { $this->evolve($evolution); } public function evolve(PokemonEvolution $evolution) { $this->evolution = $evolution; } public function damage($points) { $this->evolution->damage($points); } public function attack(Pokemon $pokemon) { $this->evolution->attack($pokemon); } } abstract class PokemonEvolution { protected $height; protected $width; protected $species; protected $hp; protected $attack; protected $defense; protected $specialAttack; protected $specialDefense; protected $speed; public function damage($points) { $this->hp -= ($points - ($this->defense + $this->specialDefense) / 2); } public function attack(Pokemon $pokemon) { $pokemon->damage($this->attack); } public function specialAttack(Pokemon $pokemon) { $pokemon->damage($this->specialAttack); } } $pikachu = new Pokemon(new Pikachu()); $bulbasaur = new Pokemon(new Bulbasaur()); $pikachu->attack($bulbasaur); $pikachu->evolve(new Raichu()); $pikachu->specialAttack($bulbasaur); // K.O. class PokeBall { private $db; public function store($pokemon) {/* INSERT INTO .. */} public function retrieve($pokemon) {/* SELECT .. */} } $pokeball = new PokeBall($db); $pikachu = $pokeball->retrieve('Pikachu'); $bulbasaur = $pokeball->retrieve('Bulbasaur'); $pikachu->attack($bulbasaur); $pikachu->evolve(new Raichu()); $pikachu->specialAttack($bulbasaur); // K.O. This isn't a finished design, a lot needs fixing but you get the general idea. Link to comment https://forums.phpfreaks.com/topic/222059-oop-arrays-help-please/#findComment-1148988 Share on other sites More sharing options...
Pikachu2000 Posted December 18, 2010 Share Posted December 18, 2010 I shall use the much loved pokemon series to explain my dilemma. The Pikachu refuses to evolve . . . Link to comment https://forums.phpfreaks.com/topic/222059-oop-arrays-help-please/#findComment-1149006 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.