Class instances on PHP?
<p>I don't know what it is called as, I believe it is instances.</p>

<p>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.</p>

<p>I think about something like this:</p>

<pre><code>$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
</code></pre>

<p>So, let's say I want to calculate the total HP of all monsters.</p>

<pre><code>$totalHp = $monster_1-&gt;getHp() + $monster_2-&gt;getHp() + $monster_3-&gt;getHp();
</code></pre>

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

<p>I could make an array instead of <code>$monster</code> variables, and calculate the $totalHp with foreach - but it looks weird... </p>

<p>I don't know how it is being done in other languages. </p>

<p>Is there any other way to archieve this?</p>

<p>Ps. I'm trying to get the logic behind "<a href="http://www.scirra.com/demos/ghosttutorial/" rel="nofollow">http://www.scirra.com/demos/ghosttutorial/</a>" 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.</p>
