Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 649779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:58:53+00:00 2026-05-13T21:58:53+00:00

I’m trying to use a foreach loop for an array of objects. Inside of

  • 0

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: 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

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

    }

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:58:53+00:00Added an answer on May 13, 2026 at 9:58 pm

    The error message indicates that you are trying to all the BattleInitiated() method on something that wasn’t an object.

    Judging from your code, the problem seems to be with this loop, in the BeginBattle() method :

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

    Which means $player, at least one in your array, is probably not an object ; maybe it’s null, or an array ?

    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 :

    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…

    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.

    To help with that, you could use var_dump on the $player being added :

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 343k
  • Answers 343k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer xsdg of #ruby worked this out Looks like that gsub's… May 14, 2026 at 5:29 am
  • Editorial Team
    Editorial Team added an answer If the image is fix you can hard-code it in… May 14, 2026 at 5:29 am
  • Editorial Team
    Editorial Team added an answer This was solved by adding a converter to the data… May 14, 2026 at 5:29 am

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.