2010-03-05 56 views
2

我想爲對象數組使用foreach循環。在BeginBattle()方法的內部,我想遍歷所有對象並自動增加它們的播放次數。不幸的是,Web瀏覽器顯示我有一個錯誤:致命錯誤:調用/nfs/c05/h01/mnt/70299/domains/munchkinparty.neededspace.net/html/中的非對象的成員函數BattleInitiated() 75線上的Battle.php通過一組對象的PHP foreach

任何想法?

<?php 
/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 

/** 
* Description of Battle 
* 
* @author joshualowry 
*/ 
class Battle { 
    /** 
    * 
    * @var <type> 
    */ 
    private $_players; 
    /** 
    * 
    * @var <type> 
    */ 
    private $_battleInProgress; 
    /** 
    * 
    */ 
    public function Battle(){ 
     $this->_players = array(); 
     $this->_battleInProgress = FALSE; 

    } 
    /** 
    * 
    * @param <type> $player 
    * @return <type> 
    */ 
    public function AddPlayer($player){ 
     if(!$this->_battleInProgress) 
      $this->_players[] = $player; 
     else 
      return; 
      //Spit some error 
    } 

    /** 
    * 
    * @param <type> $player 
    * @return <type> 
    */ 
    public function BattleWon($player){ 
     if($player != NULL) 
      $player->BattleWon(); 
     else 
      return; 
      //Spit some error 
    } 
    /** GetPlayerByName Get the player's object by the player's name field. 
    * 
    * @param <type> $playerName 
    * @return <type> 
    */ 
    public function GetPlayerByName($playerName){ 
     foreach($this->_players as &$player) { 
      if($player->GetName() == $playerName) 
     return $player; 
     } 
     return NULL; 
    } 

    /** 
    * 
    */ 
    public function BeginBattle(){ 
     $this->_battleInProgress = TRUE; 
     foreach($this->_players as $player){ 
     $player->BattleInitiated(); 
    } 
    } 
    /** 
    * 
    */ 
    public function DisplayCurrentBoard() { 
     echo "Name Alias Wins Battles<br/>"; 
     foreach($this->_players as &$player){ 
      echo "$player->GetName() $player->GetAlias() $player->GetWins() $player->GetBattles()<br/>"; 
     } 
    } 

} 
?> 

這是一切聲明,並呼籲:

<?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->AddPlayer($playerA); 
    $currentBattle->AddPlayer($playerB); 
    $currentBattle->AddPlayer($playerC); 

    $currentBattle->BeginBattle(); 
    $currentBattle->BattleWon($currentBattle->GetPlayerByName("Josh")); 
    $currentBattle->DisplayCurrentBoard(); 
?> 

玩家

<?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->_alias = $alias; 
      $this->_battles = $battles; 

      if($battles == 0) { 
       $this->_wins = 0; 
      } 
      else { 
       $this->_wins = $wins; 
      } 
     } 

     protected function SetAlias($value){ 
      $this->_alias = $value; 
     } 

     public function GetAlias(){ 
      return $this->_alias; 
     } 

     protected function SetBattles($value) { 
      $this->_battles = $value; 
     } 

     public function GetBattles(){ 
      return $this->_battles; 
     } 

     protected function SetWins($value) { 
      $this->_wins = $value; 
     } 

     public function GetWins() { 
      return $this->_wins; 
     } 

     public function BattleWon(){ 
      $this->_wins += 1; 
     } 

     public function BattleInitiated(){ 
      $this->_battles += 1; 
     } 

    } 

?> 
+0

告訴我們在哪兒AddPlayer是被調用。 – 2010-03-05 05:41:51

+0

你傳遞給AddPlayer()的是什麼? – Amber 2010-03-05 05:42:36

+0

你如何實例化你的'Battle()'對象並調用'AddPlayer()'和'BeginBattle()'函數? – 2010-03-05 05:46:14

回答

3

該錯誤消息表明您正在嘗試所有BattleInitiated()方法上WASN東西是一個對象。

從你的代碼來看,問題似乎與這個循環中,在BeginBattle()方法:

foreach($this->_players as $player){ 
    $player->BattleInitiated(); 
} 

這意味着$球員,你的陣列中的至少一個,可能不是一個對象;也許它是null,或數組?


要知道更多,你應該使用var_dump循環之前顯示的$this->_players的內容,只是爲了確保它包含了你指望它什麼:

public function BeginBattle(){ 
    var_dump($this->_players); 
    $this->_battleInProgress = TRUE; 
    foreach($this->_players as $player){ 
     $player->BattleInitiated(); 
    } 
} 

如果$this->_players不包含什麼你指望它(它可能沒有!),你再要找出爲什麼...


考慮$this->_playersAddPlayer()方法修改,該方法將接收到的數據添加到數組的末尾,我敢打賭AddPlayer()被調用至少一次,而沒有正確的$player作爲參數。

要幫助的是,你可以在$player使用var_dump添加:

public function AddPlayer($player){ 
    var_dump($player); 
    if(!$this->_battleInProgress) 
     $this->_players[] = $player; 
    else 
     return; 
     //Spit some error 
} 

如果var_dump表明至少有一次是$player不是一個對象(比如,它的null或數組,或一個字符串,...),這是您的致命錯誤的原因。

+0

我在上面添加了一些代碼,但第三個玩家PlayerC在使用AddPlayer添加後是空的......任何想法? – 2010-03-06 04:21:08

+0

我剛剛意識到我正在添加$ PlayerC而不是$ playerC。 你的答案幫我找到了錯誤。 當玩家加入戰鬥中時,我添加了一張支票來查看它是否是「玩家」。現在不會添加NULL變量和其他類型。 – 2010-03-06 04:30:01

+0

很高興看到你發現了什麼導致問題:-)並感謝解釋它:-) – 2010-03-06 12:16:56

0

$player變量或者是或不是對象你希望它是類型的

PlayerObject是你的球員的職業名稱。

例如

$battle=new Battle(); 
$player1=new PlayerObject(); 
$player2="player"; 
$battle->AddPlayer($player1); 
$battle->AddPlayer($player2); 
$battle->BeginBattle(); 
當你調用 BeginBattle()

$player1->BattleInitiated();會成功,但$player2->BattleInitiated()會給你致命錯誤,停止運行代碼。如果$player2爲空,則爲相同的整數或非PlayerObject

1

你不明白嗎?

這一切都因爲一個小錯字:

$playerA = new Player("JohnnyDanger","John",0,0); 
    $playerB = new Player("JoshTheJest","Josh",0,0); 
    $PlayerC = new Player("CarbQueen","Nicole",0,0); 

    $currentBattle->AddPlayer($playerA); 
    $currentBattle->AddPlayer($playerB); 
    $currentBattle->AddPlayer($playerC); 

聲明:$ _P_layerC 使用:$ _p_layerC

正確的,而你是好去

+0

感謝您回顧這一點。正如您將看到我對接受的答案的評論,指出海報的提示幫助我找到了錯字,並且幫助我認識到我需要在添加條目之前驗證條目。 – 2013-02-21 15:13:31