V34 Posted May 28, 2007 Share Posted May 28, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/53302-solved-oop-php-4-5-problem/ Share on other sites More sharing options...
448191 Posted May 28, 2007 Share Posted May 28, 2007 This makes no sense: $man->health > 0 && $woman->health > 0 Where are these $man and $woman variables coming from? Quote Link to comment https://forums.phpfreaks.com/topic/53302-solved-oop-php-4-5-problem/#findComment-263420 Share on other sites More sharing options...
V34 Posted May 28, 2007 Author Share Posted May 28, 2007 $man = new Person(); $woman = new Person(); Quote Link to comment https://forums.phpfreaks.com/topic/53302-solved-oop-php-4-5-problem/#findComment-263423 Share on other sites More sharing options...
448191 Posted May 28, 2007 Share Posted May 28, 2007 Well duh, but those are not in the same scope as that line. Quote Link to comment https://forums.phpfreaks.com/topic/53302-solved-oop-php-4-5-problem/#findComment-263425 Share on other sites More sharing options...
V34 Posted May 28, 2007 Author Share Posted May 28, 2007 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/53302-solved-oop-php-4-5-problem/#findComment-263428 Share on other sites More sharing options...
448191 Posted May 28, 2007 Share Posted May 28, 2007 Try using &new. Quote Link to comment https://forums.phpfreaks.com/topic/53302-solved-oop-php-4-5-problem/#findComment-263435 Share on other sites More sharing options...
V34 Posted May 28, 2007 Author Share Posted May 28, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/53302-solved-oop-php-4-5-problem/#findComment-263440 Share on other sites More sharing options...
448191 Posted May 28, 2007 Share Posted May 28, 2007 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/53302-solved-oop-php-4-5-problem/#findComment-263443 Share on other sites More sharing options...
V34 Posted May 28, 2007 Author Share Posted May 28, 2007 Neither do I, but if change my PHP version from 5 to 4, it will print 100 | 100 infinity. Quote Link to comment https://forums.phpfreaks.com/topic/53302-solved-oop-php-4-5-problem/#findComment-263452 Share on other sites More sharing options...
448191 Posted May 28, 2007 Share Posted May 28, 2007 What do you mean by 'infinity'? Edit: please show the EXACT output. Quote Link to comment https://forums.phpfreaks.com/topic/53302-solved-oop-php-4-5-problem/#findComment-263457 Share on other sites More sharing options...
V34 Posted May 28, 2007 Author Share Posted May 28, 2007 100 | 100 100 | 100 100 | 100 100 | 100 It never ends, it keep loading and continuing 100 | 100. Quote Link to comment https://forums.phpfreaks.com/topic/53302-solved-oop-php-4-5-problem/#findComment-263461 Share on other sites More sharing options...
448191 Posted May 28, 2007 Share Posted May 28, 2007 I see. So you get stuck in a loop because Person::health never decrements. Quote Link to comment https://forums.phpfreaks.com/topic/53302-solved-oop-php-4-5-problem/#findComment-263466 Share on other sites More sharing options...
448191 Posted May 28, 2007 Share Posted May 28, 2007 Try this: while ($man->health > 0 && $woman->health > 0) { $man->attack(&$woman); $woman->attack(&$man); echo $man->health." | ".$woman->health."<hr>"; } Quote Link to comment https://forums.phpfreaks.com/topic/53302-solved-oop-php-4-5-problem/#findComment-263469 Share on other sites More sharing options...
V34 Posted May 28, 2007 Author Share Posted May 28, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/53302-solved-oop-php-4-5-problem/#findComment-263471 Share on other sites More sharing options...
448191 Posted May 28, 2007 Share Posted May 28, 2007 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()). Quote Link to comment https://forums.phpfreaks.com/topic/53302-solved-oop-php-4-5-problem/#findComment-263478 Share on other sites More sharing options...
V34 Posted May 28, 2007 Author Share Posted May 28, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/53302-solved-oop-php-4-5-problem/#findComment-263479 Share on other sites More sharing options...
448191 Posted May 28, 2007 Share Posted May 28, 2007 OOP on php4 is a bit tweaky. The golden rule is to use the 'reference operator' (&) whenever you pass an object, unless you intend to clone it. Quote Link to comment https://forums.phpfreaks.com/topic/53302-solved-oop-php-4-5-problem/#findComment-263483 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.