简体   繁体   English

PHP foreach通过一系列对象

[英]PHP foreach over an array of objects

I'm trying to use a foreach loop for an array of objects. 我正在尝试对一组对象使用foreach循环。 Inside of the BeginBattle() method I want to iterate through all of the objects and increment their played count automatically. 在BeginBattle()方法内部,我想遍历所有对象并自动增加其播放计数。 Unfortunately, the web browser shows I have an error: 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 不幸的是,Web浏览器显示我有一个错误: 致命错误:在/nfs/c05/h01/mnt/70299/domains/munchkinparty.neededspace.net/html/Battle中的非对象上调用成员函数BattleInitiated() .php行75

Any ideas? 有任何想法吗?

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

}
?>

This is where everything is declared and called: 在这里声明和调用所有内容:

<?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();
?>

The Player Class 玩家阶层

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

    }

?>

The error message indicates that you are trying to all the BattleInitiated() method on something that wasn't an object. 该错误消息表明您正在尝试对不是对象的某些对象使用所有BattleInitiated()方法。

Judging from your code, the problem seems to be with this loop, in the BeginBattle() method : 从您的代码来看,问题似乎出在BeginBattle()方法中的此循环中:

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

Which means $player, at least one in your array, is probably not an object ; 这意味着$ player,可能在您的数组中至少一个不是对象; maybe it's null , or an array ? 也许它为null或数组?


To know more, you should use var_dump to display the content of $this->_players before the loop, just to make sure it contains what you expect it to : 要了解更多,您应该使用var_dump在循环之前显示$this->_players var_dump $this->_players的内容,只是要确保它包含您期望的内容:

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

If $this->_players doesn't contain what you expect it to (and it probably doesn't !) , you'll then have to find out why... 如果$this->_players不包含您期望的内容(并且可能不包含!) ,则必须找出原因...


Considering $this->_players is modified by the AddPlayer() method, which adds what it receives to the end of the array, I would bet that AddPlayer() is called at least once without a correct $player as a parameter. 考虑到$this->_players AddPlayer() $this->_players是由AddPlayer()方法修改的,该方法会将接收到的内容添加到数组的末尾,因此我敢打赌,至少在没有正确的$player作为参数的情况下,调用AddPlayer()至少一次。

To help with that, you could use var_dump on the $player being added : 为了解决这个问题,您可以在要添加的$player上使用var_dump

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

If that var_dump indicates at least once that $player is not an object (for instance, it's null , or an array, or a string, ...) , that's the cause of your Fatal Error. 如果该var_dump至少一次表明$player不是对象(例如,它为null ,数组或字符串,...) ,则这是导致致命错误的原因。

don't you see it?? 你没看到吗?

it's all because of a small typo: 都是因为打错字了:

 $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);

declared: $_P_layerC used: $_p_layerC 声明:$ _P_layerC使用:$ _p_layerC

correct that and you're good to go 改正它,你很好

Your $player variable is either null or not an Object of the type you want it to be. 您的$player变量为null或不是您想要的类型Object

PlayerObject is what ever your class name for player is. PlayerObject是您的播放器类别名称。

For example 例如

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

when you call BeginBattle() the $player1->BattleInitiated(); 当您调用BeginBattle()$player1->BattleInitiated(); will be successful but the $player2->BattleInitiated() will give you the fatal error and stop your code from running. 将成功,但$player2->BattleInitiated()将给您致命错误并停止代码运行。 same if $player2 was null, an integer or something that is not PlayerObject . 如果$player2为null,整数或不是PlayerObject东西,则PlayerObject

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM