Kitu Posted February 4, 2007 Share Posted February 4, 2007 Hi, I am writing my first script using OOP, however there is a problem I can't solve no matter how many tutorials/manuals I read, as it just looks fine to me and returns no error message(s). var $tile; function setTile( $symbol ) { $this->tile=$symbol; } function getTile() { return $this->tile; } It's just regular geter/seter stuff, but it just refuses to work...It is probably just some small, stupid error, but I still can't find it :\ Quote Link to comment https://forums.phpfreaks.com/topic/37054-problem-with-php-class/ Share on other sites More sharing options...
wildteen88 Posted February 4, 2007 Share Posted February 4, 2007 Works fine for me: <?php class tileCls { var $tile; function setTile($symbol) { $this->tile = $symbol; } function getTile() { return $this->tile; } } $tile = new tileCls; $tile->setTile('my tile'); echo $tile->getTile(); // returns: 'my tile' ?> How are you initiating the class? Quote Link to comment https://forums.phpfreaks.com/topic/37054-problem-with-php-class/#findComment-176943 Share on other sites More sharing options...
Kitu Posted February 4, 2007 Author Share Posted February 4, 2007 These 2 functions are just parts of a way bigger class and both of the functions are only used internally. Quote Link to comment https://forums.phpfreaks.com/topic/37054-problem-with-php-class/#findComment-176962 Share on other sites More sharing options...
wildteen88 Posted February 4, 2007 Share Posted February 4, 2007 OK. How are calling these function in the class? In order to call a method (function) that is in the same class you do this: $this->myFunctionName(); So to call the setTile function you do this: $this->setTile('my tile'); $this-> is is used to access methods and objects (variables) that in the same class. Quote Link to comment https://forums.phpfreaks.com/topic/37054-problem-with-php-class/#findComment-176964 Share on other sites More sharing options...
Kitu Posted February 4, 2007 Author Share Posted February 4, 2007 Thanx for answering so fast The problem is, that I am currently using them like that...I'll copy the exact lines just in case though. In one function: $this->setTile($map_layer[$a][$b]); And in other: $monster = new fight ( $this->getTile() ); Quote Link to comment https://forums.phpfreaks.com/topic/37054-problem-with-php-class/#findComment-176972 Share on other sites More sharing options...
wildteen88 Posted February 4, 2007 Share Posted February 4, 2007 You will need to show more code in order for us to see how your class works. Preferably post the whole class here. Make sure you use the code tags ( ) when you post the code for the class. Quote Link to comment https://forums.phpfreaks.com/topic/37054-problem-with-php-class/#findComment-176984 Share on other sites More sharing options...
Kitu Posted February 7, 2007 Author Share Posted February 7, 2007 I managed to solve the last situation simply by eliminating the variable and adding a field to MySql...but I still have the same problem with other class....set_opponent_hp() and set_player_hp() do absolutly nothing. <?php class fight { var $player_hp, $opponent_hp, $opponent_stats; /* Contructor taksed 3 arguments: opponent - 'player' or 'monster'...determines weather the opponent is player (pvp) or monster. Meaning, it tells the script whitch table it should query for opponents stats." Opponent id is the id of opponent...if opponent is monster, it holds monster id, if opponent is other player it holds his/her id. Finally, attacker field determines who is the one attackig (and therefore gets the first hit. Holds 2 values, eather 'player' or 'opponent'.*/ function fight($opponent, $opponent_id, $attacker) { $this->set_opponent_hp( $opponent, $opponent_id ); $this->set_player_hp(); while ( $this->get_player_hp() > 0 && $this->get_opponent_hp > 0 ) //While boath participants have hitpoints left { echo 'Mõlemad on elus!<br />'; if ( $attacker == 'player' ) //If player is the one attacking { $this->set_opponent_stats($opponent, $opponent_id); $this->attack($this->calculate_hit($opponent, $opponent_id), 'player'); //Let the player hit the opponent if ( $this->get_opponent_hp() ) //if the opponent still is alive { $this->set_opponent_stats('player', $_SESSION['User']); $this->attack($this->calculate_hit($opponent, $opponent_id), 'opponent'); //let the opponent hit } else //if the opponent died { break; //exit the loop } } else { $this->set_opponent_stats('player', $_SESSION['User']); $this->attack($this->calculate_hit($opponent)); //Let the opponent hit the player if ( $this-> get_player_hp() ) //if the player still is alive { $this->set_opponent_stats($opponent, $opponent_id); $this->attack($this->calculate_hit('player')); //let the player hit } else //if the player died { break; //exit the loop } } } } function set_opponent_stats($opponent, $opponent_id) { if ($opponent == 'monster') //if the one hiting is monster $stats = mysql_fetch_assoc(mysql_query ( "SELECT * FROM monsters WHERE id='".$opponent_id."'") );//get the monster stats else //If the attacker is other player $stats = mysql_fetch_assoc(mysql_query ( "SELECT * FROM user_stats WHERE id='".$opponent_id."'") ); //get the player stats $this->opponent_stats = $stats['hitpoints']; } function set_player_hp() { $stats = mysql_fetch_assoc(mysql_query ( "SELECT * FROM user_stats WHERE id='".$_SESSION['User']."'") ); //get the player stats $this->player_hp = $stats['hitpoints']; } function get_player_hp() { return $this->player_hp; } function get_opponent_hp() { return $this->opponent_hp; } function set_opponent_hp( $opponent, $opponent_id ) //Gets the stats of "Opposing force", no matter weather the one hiting is player or monster { if ($opponent == 'monster') //if the one hiting is monster $stats = mysql_fetch_assoc(mysql_query ( "SELECT * FROM monsters WHERE id='".$opponent_id."'"));//get the monster stats else //If the attacker is other player $stats = mysql_fetch_assoc(mysql_query ( "SELECT * FROM user_stats WHERE id='".$opponent_id."'")); //get the player stats $this->opponent_stats = $stats; } function get_opponent_stats() //Returns the opponent stats { return $this->opponent_stats; } function calculate_hit( $attacker, $attacker_id ) { if ($attacker == 'monster') //if the one hiting is monster $stats = mysql_fetch_assoc(mysql_query( "SELECT * FROM monsters WHERE id='".$attacker_id."'"));//get the monster stats else //If the attacker is other player $stats = mysql_fetch_assoc(mysql_query( "SELECT * FROM user_stats WHERE id='".$attacker_id."'")); //get the player stats if ( $this->check_if_hit($stats['attack'],$stats['luck']) ) //checks if the attacker hit or missed { srand(time()); $hit = (rand()%$stats['attack']+$stats['agility']); return $hit; } else //if attacker missed { return 0; //return 0 as the hit value } } function check_if_hit( $accuracy, $luck ) //Calculates weather the attacker hits or not { $opponent_stats = get_opponent_stats(); //gets opponent stats $total = $opponent_stats['defence']+$accuracy; //calculates the total of opponent defence+user attack srand(time()); $random = (rand()%$total); if ( $opponent_stats['defence'] > $accuracy ) //if opponents defence is better than players defence { if ( $random < $accuracy + $luck ) //if random number is smaller than attackers attack + luck return true; //hit else //if not return false; //miss } else //if players attack is better than opponents defence { if ( $random > $accuracy - $luck ) //if random number is smaller than attackers attack - luck return true; //hit else //if not return false; //miss } } function attack( $hits, $attacker ) //substracts the hit amoount of hitpoints from player/monster hp value { if ( $attacker == 'player' ) { $this->player_hp = $this->player_hp-$hits; echo 'You hit: '.$hits; } else { $this->opponent_hp = $this->opponent_hp-$hits; echo 'Opponent hit: '.$hits; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/37054-problem-with-php-class/#findComment-179061 Share on other sites More sharing options...
trq Posted February 7, 2007 Share Posted February 7, 2007 Can we see the code where you use this class ? Quote Link to comment https://forums.phpfreaks.com/topic/37054-problem-with-php-class/#findComment-179068 Share on other sites More sharing options...
Kitu Posted February 7, 2007 Author Share Posted February 7, 2007 Can we see the code where you use this class ? I havn't actually implemented it to my game yet, I just tested it in a stand-alone file, so it just looks like: <?php session_start(); include 'db.php'; include 'classes/fight.php'; $test = new fight ('monster', 1, 'player'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/37054-problem-with-php-class/#findComment-179071 Share on other sites More sharing options...
trq Posted February 7, 2007 Share Posted February 7, 2007 Lines like this.... $stats = mysql_fetch_assoc(mysql_query ( "SELECT * FROM user_stats WHERE id='".$_SESSION['User']."'") ); really ought to be avoided. It makes it very difficult to debug. Also, NEVER try and use a result resource without checking your query actually worked. That one line should look more like. if ($result = mysql_query ( "SELECT * FROM user_stats WHERE id='".$_SESSION['User']."'")) { if (mysql_num_rows($result)) { $stats = mysql_fetch_assoc($result); } } Quote Link to comment https://forums.phpfreaks.com/topic/37054-problem-with-php-class/#findComment-179075 Share on other sites More sharing options...
Kitu Posted February 7, 2007 Author Share Posted February 7, 2007 I added error check to all of the querys, but the result still is the same...blank screen. Quote Link to comment https://forums.phpfreaks.com/topic/37054-problem-with-php-class/#findComment-179083 Share on other sites More sharing options...
trq Posted February 7, 2007 Share Posted February 7, 2007 I added error check to all of the querys, but the result still is the same...blank screen. The methods you say 'do absolutly nothing' don't actually output anything. What code are you using to call these methods, and what output do you expect? Quote Link to comment https://forums.phpfreaks.com/topic/37054-problem-with-php-class/#findComment-179096 Share on other sites More sharing options...
Kitu Posted February 7, 2007 Author Share Posted February 7, 2007 I'm not actually expecting any output from these methods (except when error occures, then I expect it to output the error message), they are just supposed to set values to the variables...and, in some reason, they fail to do that and don't output any errors. Quote Link to comment https://forums.phpfreaks.com/topic/37054-problem-with-php-class/#findComment-179116 Share on other sites More sharing options...
trq Posted February 7, 2007 Share Posted February 7, 2007 Really, the way you have this class setup makes it pretty difficult to debug. Why are you calling all the methods within the construct instead of outside in client code? Your going to need to place some sort of debug messages in the places where this thing might fail. Without those we can't really help. Your supplying absolutely no usefull infomation. Quote Link to comment https://forums.phpfreaks.com/topic/37054-problem-with-php-class/#findComment-179137 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.