oni-kun Posted May 9, 2009 Share Posted May 9, 2009 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.. Quote Link to comment https://forums.phpfreaks.com/topic/157444-how-do-i-iterate-through-created-classes/ Share on other sites More sharing options...
trq Posted May 9, 2009 Share Posted May 9, 2009 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/157444-how-do-i-iterate-through-created-classes/#findComment-830004 Share on other sites More sharing options...
oni-kun Posted May 9, 2009 Author Share Posted May 9, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/157444-how-do-i-iterate-through-created-classes/#findComment-830006 Share on other sites More sharing options...
trq Posted May 9, 2009 Share Posted May 9, 2009 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(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/157444-how-do-i-iterate-through-created-classes/#findComment-830012 Share on other sites More sharing options...
oni-kun Posted May 9, 2009 Author Share Posted May 9, 2009 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 , 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. Quote Link to comment https://forums.phpfreaks.com/topic/157444-how-do-i-iterate-through-created-classes/#findComment-830019 Share on other sites More sharing options...
Maq Posted May 9, 2009 Share Posted May 9, 2009 After this point my text RPG game looks fairly easy to accomplish, thanks again. Nice, what's the game about? (if you don't mind me asking...) Quote Link to comment https://forums.phpfreaks.com/topic/157444-how-do-i-iterate-through-created-classes/#findComment-830035 Share on other sites More sharing options...
Mark Baker Posted May 9, 2009 Share Posted May 9, 2009 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"; } } Quote Link to comment https://forums.phpfreaks.com/topic/157444-how-do-i-iterate-through-created-classes/#findComment-830169 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.