ILYAS415 Posted May 10, 2008 Share Posted May 10, 2008 Hey thanks for looking. Well basically I am new to classes and im trying to echo some class variables. Okay at the moment, after rearranging my code i have been succesfull. Heres my code... <head> <? include("includes/engine.php"); ?> </head> <body> <? $azeem = new enemy(); $azeem->set_name("Azeem Shezad Ilyas"); echo "".$azeem->get_name()." comes flying at you with <b>fists</b><br />"; $azeem->set_health(1); echo "His health is ".$azeem->get_health()."! LMAO<br />"; ?> </body> However i wish to rearrange it so that the $azeem->set_health(1); is exactly below the $azeem->set_name("blablabla"); class function. The problem is that when i do so the name is also set to 1 (or watever the health var is set to. Heres my coding for my class construct... <? class enemy{ //CLASS FOR ENEMY var $name; var $health; var $maxhealth; var $attack; //FUNCTIONS FOR GIVING THE ENEMY STATS function set_name($var1){ $this->$name = $var1; } function set_health($var2){ $this->$health = $var2; } function set_maxhealth($var3){ $this->$maxhealth = $var3; } function set_attack($var4){ $this->$attack = $var4; } //NOW TIME FOR GETTING THE ENEMY STATS function get_name(){ return $this->$name; } function get_health(){ return $this->$health; } function get_maxhealth(){ return $this->$maxhealth; } function get_attack(){ return $this->$attack; } } //END OF CLASS FOR ENEMY ?> There are no errors or anything so i dont know what is wrong. Its just that when i rearrange this bit of my previous code... <? $azeem = new enemy(); $azeem->set_name("Azeem Shezad Ilyas"); echo "".$azeem->get_name()." comes flying at you with <b>fists</b><br />"; $azeem->set_health(1); echo "His health is ".$azeem->get_health()."! LMAO<br />"; ?> to... <? $azeem = new enemy(); $azeem->set_name("Azeem Shezad Ilyas"); $azeem->set_health(1); echo "".$azeem->get_name()." comes flying at you with <b>fists</b><br />"; echo "His health is ".$azeem->get_health()."! LMAO<br />"; ?> then the name of the class is also set to 1. Any health would be greatly appreciated. Quote Link to comment Share on other sites More sharing options...
ILYAS415 Posted May 10, 2008 Author Share Posted May 10, 2008 Anyone there ? Quote Link to comment Share on other sites More sharing options...
rarebit Posted May 10, 2008 Share Posted May 10, 2008 One thing i've noticed: function get_name(){ return $this->$name; } needs to to be changed to: function get_name(){ return $this->name; } [code] notice the class element doesnt have a $, this is all the way through the class... [/code] Quote Link to comment Share on other sites More sharing options...
ILYAS415 Posted May 10, 2008 Author Share Posted May 10, 2008 Thanks i think its working now! Quote Link to comment 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.