Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP foreach over an array of objects
    primarykey
    data
    text
    <p>I'm trying to use a foreach loop for an array of objects. Inside of the BeginBattle() method I want to iterate through all of the objects and increment their played count automatically. Unfortunately, the web browser shows I have an error: <strong>Fatal error: Call to a member function BattleInitiated() on a non-object in /nfs/c05/h01/mnt/70299/domains/munchkinparty.neededspace.net/html/Battle.php on line 75</strong></p> <p>Any ideas?</p> <pre><code>&lt;?php /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * Description of Battle * * @author joshualowry */ class Battle { /** * * @var &lt;type&gt; */ private $_players; /** * * @var &lt;type&gt; */ private $_battleInProgress; /** * */ public function Battle(){ $this-&gt;_players = array(); $this-&gt;_battleInProgress = FALSE; } /** * * @param &lt;type&gt; $player * @return &lt;type&gt; */ public function AddPlayer($player){ if(!$this-&gt;_battleInProgress) $this-&gt;_players[] = $player; else return; //Spit some error } /** * * @param &lt;type&gt; $player * @return &lt;type&gt; */ public function BattleWon($player){ if($player != NULL) $player-&gt;BattleWon(); else return; //Spit some error } /** GetPlayerByName Get the player's object by the player's name field. * * @param &lt;type&gt; $playerName * @return &lt;type&gt; */ public function GetPlayerByName($playerName){ foreach($this-&gt;_players as &amp;$player) { if($player-&gt;GetName() == $playerName) return $player; } return NULL; } /** * */ public function BeginBattle(){ $this-&gt;_battleInProgress = TRUE; foreach($this-&gt;_players as $player){ $player-&gt;BattleInitiated(); } } /** * */ public function DisplayCurrentBoard() { echo "Name Alias Wins Battles&lt;br/&gt;"; foreach($this-&gt;_players as &amp;$player){ echo "$player-&gt;GetName() $player-&gt;GetAlias() $player-&gt;GetWins() $player-&gt;GetBattles()&lt;br/&gt;"; } } } ?&gt; </code></pre> <p>This is where everything is declared and called:</p> <pre><code>&lt;?php include 'Battle.php'; include 'Person.php'; include 'Player.php'; $currentBattle = new Battle(); $playerA = new Player("JohnnyDanger","John",0,0); $playerB = new Player("JoshTheJest","Josh",0,0); $PlayerC = new Player("CarbQueen","Nicole",0,0); $currentBattle-&gt;AddPlayer($playerA); $currentBattle-&gt;AddPlayer($playerB); $currentBattle-&gt;AddPlayer($playerC); $currentBattle-&gt;BeginBattle(); $currentBattle-&gt;BattleWon($currentBattle-&gt;GetPlayerByName("Josh")); $currentBattle-&gt;DisplayCurrentBoard(); ?&gt; </code></pre> <p>The Player Class</p> <pre><code> &lt;?php /** * Description of Player * * @author joshualowry */ class Player extends Person { private $_alias; private $_wins; private $_battles; public function Player($name, $alias, $wins, $battles) { parent::SetName($name); $this-&gt;_alias = $alias; $this-&gt;_battles = $battles; if($battles == 0) { $this-&gt;_wins = 0; } else { $this-&gt;_wins = $wins; } } protected function SetAlias($value){ $this-&gt;_alias = $value; } public function GetAlias(){ return $this-&gt;_alias; } protected function SetBattles($value) { $this-&gt;_battles = $value; } public function GetBattles(){ return $this-&gt;_battles; } protected function SetWins($value) { $this-&gt;_wins = $value; } public function GetWins() { return $this-&gt;_wins; } public function BattleWon(){ $this-&gt;_wins += 1; } public function BattleInitiated(){ $this-&gt;_battles += 1; } } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload