PHP预测了一系列对象

时间:2010-03-05 05:38:03

标签: php arrays foreach iteration

我正在尝试使用foreach循环来处理对象数组。在BeginBattle()方法内部,我希望遍历所有对象并自动增加其播放计数。不幸的是,Web浏览器显示我有一个错误:致命错误:在/nfs/c05/h01/mnt/70299/domains/munchkinparty.neededspace.net/上的非对象上调用成员函数BattleInitiated()第75行的html / Battle.php

有什么想法吗?

<?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;
        }

    }

?>

3 个答案:

答案 0 :(得分:3)

错误消息表明您正在尝试对非对象的所有BattleInitiated()方法。

从您的代码判断,问题似乎是在这个循环中,在BeginBattle()方法中:

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

这意味着$ player,你的数组中至少有一个,可能不是一个对象;也许是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 }作为参数。

为此,您可以在添加的var_dump上使用$player

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

如果var_dump至少表明$player不是对象(例如,它是null,或者数组或字符串,...) ,这是致命错误的原因。

答案 1 :(得分: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

纠正这一点,你很高兴

答案 2 :(得分:0)

您的$player变量是 null ,或者不是您想要的类型对象

PlayerObject就是玩家的班级名称。

例如

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

当您致电BeginBattle()时,$player1->BattleInitiated();将成功,但$player2->BattleInitiated()会给您致命错误并停止运行您的代码。如果$player2为null,则为整数或不是PlayerObject的内容。