Jump to content

How do I iterate through created classes?


oni-kun

Recommended Posts

For my first php project that I wanted to make, it is a simple game that includes a monster class.

 

class Monster {

(public variables, e.g. name) 

(functions...)

}

 

So I can call..

$rat = new Monster()

$rat->name = "Rat"

$rat->health = 20

 

How do I use foreach to list all created monsters?

 

foreach ($monster as $key => $name) {

    echo "Monster: $key; Name: $name<br />\n";

}

 

Or similar, but none have worked since it's a class and not an array.. I wish just to list all of the Monsters using that class, and with that their names/attributes..

 

You'll need to post your code. You can iterate through member variables using a foreach without issues. eg;

 

<?php

class foo
{
    public $a;
    public $b;

}

$foo = new foo;
$foo->a = 'this is a';
$foo->b = 'this is b';

foreach ($foo as $val) {
    echo $val . "\n";
}

You'll need to post your code. You can iterate through member variables using a foreach without issues. eg;

 

That works on something like..

 

$rat = new Monster

$rat->name = ...

 

foreach ($rat as $val) {

    echo $val . "\n";

}

 

But how do you list everything using the class Monster?

 

class Monster -> $rat

class Monster -> $skeleton

 

How do you list all 'monsters' ( e.g. their names ) that are using the Monster class?

You don't. They are in no way related. Just because I'm of the type 'human' does not mean I'm related to you.

 

Now, you might have another class (called monsters) that stores all your 'monster' objects.

 

<?php

class monster
{
    private $_name;

    public function __construct($name)
    {
        $this->_name = $name;
    }

    public function getname()
    {
        return $this->_name;
    }

}

class monsters
{
    private $_data;

    public function add(monster $monster) {
        $this->_data[] = $monster;
    }

    public function getall()
    {
        return $this->_data;
    }
}

$monsters = new monsters;
$monsters->add(new monster('foo'));
$monsters->add(new monster('bar'));
$monsters->add(new monster('bob'));

foreach ($monsters->getall() as $monster) {
    echo $monster->getname();
}

?>

You don't. They are in no way related. Just because I'm of the type 'human' does not mean I'm related to you.

 

Now, you might have another class (called monsters) that stores all your 'monster' objects.

 

code

 

Thanks a million  ;D , As i'm a newbie to programming, I get how to do them, but I don't know the greater uses, in higher applications..

 

After this point my text RPG game looks fairly easy to accomplish, thanks again.

I doubt that it would be very efficient in practice, and it may not even work,  but an attempt to answer the original query:

 

//  get a list of all defined variables
$varList = get_defined_vars();
foreach($varList as $var) {
   $testing = &$$var;
   if ((is_object($testing)) && (get_class($testing) == 'Monster')) {
      echo "Monster: ".$var." Name: ".$testing->name"<br />\n";
   }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.