Jump to content

[SOLVED] Simple class problem


ILYAS415

Recommended Posts

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.

 

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.