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

  • SEARCH
  • Home
  • 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 8053995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:02:05+00:00 2026-06-05T08:02:05+00:00

I don’t know what it is called as, I believe it is instances. Let’s

  • 0

I don’t know what it is called as, I believe it is instances.

Let’s say we have a “Monster” class. This class should contain “Health” value. It is set to “5” on class creation. It should also contain an unique ID value for each monster.

I think about something like this:

$monster_1 = new Monster(); //sets monster ID to 1
$monster_2 = new Monster(); //sets monster ID to 2
$monster_3 = new Monster(); //sets monster ID to 3

So, let’s say I want to calculate the total HP of all monsters.

$totalHp = $monster_1->getHp() + $monster_2->getHp() + $monster_3->getHp();

It will also work, but what if I add a new monster named $monster_4. I would have to add it to $totalHp calculation manually.

I could make an array instead of $monster variables, and calculate the $totalHp with foreach – but it looks weird…

I don’t know how it is being done in other languages.

Is there any other way to archieve this?

Ps. I’m trying to get the logic behind “http://www.scirra.com/demos/ghosttutorial/” game. You create a Monster class, and each Monster uses this class as an instance. My solution would definitely work, but I’m not sure if it is a hacky way to archieve this.

  • 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-06-05T08:02:07+00:00Added an answer on June 5, 2026 at 8:02 am

    Create a helper class which holds a set of Monsters, maybe a MonsterTeam class;

    class Monster {
         ...
    }
    
    class MonsterTeam {
        private $_monsters = [];
    
        public function addMonster(Monster &$monster) {
            array_push($this->_monsters, $monster);
        }
    
        public function getTotalHp() {
            $hp = 0;
    
            foreach ($this->_monsters as $monster) {
                $hp += $monster->getHp();
            }
    
            return $hp;
        }
    }
    
    
    $team = new MonsterTeam();
    
    $monsterA = new Monster();
    $team->addMonster($monsterA);
    
    $monsterB = new Monster();
    $team->addMonster($monsterB);
    
    echo "Total HP: " . $team->getTotalHp();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

don't know better title for this, but here's my code. I have class user
Don't know if this has been answered before. Have custom routes to users. If
Don't know why but I can't find a solution to this. I have 3
Don't know why this doesn't work. I can't do implicit animation for a newly
Don't really know how to formulate the title, but it should be pretty obvious
(Don't know if this is strictly on-topic, but I don't see any better Stack
Don't know if this has been asked before, so point me to another question
Don't know if anyone can help me with this or if it's even possible.
I don't know: if this works. if it's a good idea. what it is
I don't know if this question is trivial or not. But after a couple

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.