Jump to content

[SOLVED] OOP PHP 4-5 Problem


V34

Recommended Posts

It's actually the same problem as last time, but this time I got more specific with my problem.

It seems like the Health variable don't want to change of some reason.

 

I'm still using PHP 4.

<?php
class Person {
  var $health = 100;

  function attack($perAtt) {
   if ($man->health > 0 && $woman->health > 0) {
   $perAtt->health = $perAtt->health - 25;
   }
  }

}

$man = new Person();
$woman = new Person();

while ($man->health > 0 && $woman->health > 0) {
$man->attack($woman);
$woman->attack($man);
echo "$man->health | $woman->health<hr>";
}

echo $man->health." || ".$woman->health;

?> 

 

It echo's 100 | 100 all the time, but if I change my PHP version from 4 to 5 there's no problem at all.

Now this is not a solution. I want to know how I change the variable, thanks.

 

- Jesper Eiby

Link to comment
Share on other sites

<?php
class Person {
		var $health = 100;

		function attack($perAtt) {
		 if ($this->health > 0 && $perAtt->health > 0) {
		  $perAtt->health = $perAtt->health - 25;
		 }
		}

}

$man = new Person();
$woman = new Person();

while ($man->health > 0 && $woman->health > 0) {
$man->attack($woman);
$woman->attack($man);
echo $man->health." | ".$woman->health."<hr>";
}

echo $man->health." || ".$woman->health;

?>

 

My bad, but still this improvement didn't seem to work.

Link to comment
Share on other sites

Thanks for the help, but the &new didn't seem to have any effect at all.

 

I've also tryed putting global $health; in top of the attack() function. Again with no luck

 

 

Link to comment
Share on other sites

<?php
class Person {
var $health = 100;

function attack($perAtt) {
	if ($this->health > 0 && $perAtt->health > 0) {
		$perAtt->health = $perAtt->health - 25;
	}
}
}

$man = &new Person();
$woman = &new Person();

while ($man->health > 0 && $woman->health > 0) {
$man->attack($woman);
$woman->attack($man);
echo $man->health." | ".$woman->health."<hr>";
}
echo $man->health." || ".$woman->health;
?>

 

It outputs this on php5:

 

75 | 75

50 | 50

25 | 25

25 | 0

25 || 0

 

I don't see any reason why it should output anything different on php4.

Link to comment
Share on other sites

Yes exactly.

But if I change $perAtt->health = $perAtt->health - 25; to $this->health = $this->health - 25;

It has no problem working, but then they're attacking them self.

 

Again thanks for you help.

Link to comment
Share on other sites

The above post should work. It's basic stuff, actually, but I've been off php4 for so long that I overlooked it. php4 makes copies of objects whedn you pass them, so you have to pass by reference to avoid that a copy is made (to avoid $health being set to 100 every time you attack()).

Link to comment
Share on other sites

Thanks alot! That worked!

And it also worked on PHP 5.

 

Could you explain what the & does, or do you reefer me to Google?

 

EDIT: Ahh, thanks a lot for all your help! I'm quite new in OOP but I've been programming PHP for 2years.

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.