ryanhowdy Posted April 9, 2007 Share Posted April 9, 2007 I am trying to re-write an old message board script I wrote awhile ago, using object oriented programming. The problem is I keep getting errors on one server A using php 4.4.2 and it works fine on server B using php 4.4.4. Anyone know what's wrong with my code and how/where I can find out how to fix it. PHP Notice: Undefined variable: db in .../httpdocs/inc/messageboard.class.php on line 11 PHP Notice: Undefined variable: db in .../httpdocs/inc/messageboard.class.php on line 12 PHP Notice: Undefined variable: db in .../httpdocs/inc/messageboard.class.php on line 13 PHP Notice: Undefined variable: db in .../httpdocs/inc/messageboard.class.php on line 14 <?php include 'database.class.php'; class MessageBoard { var $db; var $tz_offset; var $cur_user_id; function MessageBoard ($current_user_id, $type, $host, $database, $user, $pass) { $this->$db = new database($type, $host, $database, $user, $pass); // line 11 $this->$db->debug_mode(); // line 12 $this->$db->query("SELECT timezone FROM fcms_users WHERE id = $current_user_id"); // line 13 $row = $this->$db->get_row(); // line 14 switch($row['timezone']) { case 'EDT': $this->$tz_offset = '-4 hours'; break; case 'CDT': $this->$tz_offset = '-5 hours'; break; case 'MDT': $this->$tz_offset = '-6 hours'; break; case 'PDT': $this->$tz_offset = '-7 hours'; break; case 'AKDT': $this->$tz_offset = '-8 hours'; break; case 'HST': $this->$tz_offset = '-9 hours'; break; default: $this->$tz_offset = '-4 hours'; break; } } Quote Link to comment https://forums.phpfreaks.com/topic/46274-442-vs-444/ Share on other sites More sharing options...
utexas_pjm Posted April 9, 2007 Share Posted April 9, 2007 $this->db not $this->$db. I'm guessing you're not seeing the notices the on the other server because you are running E_ALL ^ E_NOTICE?: http://us3.php.net/error-reporting Best, Patrick Quote Link to comment https://forums.phpfreaks.com/topic/46274-442-vs-444/#findComment-225110 Share on other sites More sharing options...
ryanhowdy Posted April 9, 2007 Author Share Posted April 9, 2007 I understand that the notices are turned off, but that shouldn't stop the application from working at all should it? Quote Link to comment https://forums.phpfreaks.com/topic/46274-442-vs-444/#findComment-225145 Share on other sites More sharing options...
trq Posted April 9, 2007 Share Posted April 9, 2007 I understand that the notices are turned off, but that shouldn't stop the application from working at all should it? Notices are just that. They won't stop the application running, but will display Notices. fix your code (good), or turn notices off (bad). Quote Link to comment https://forums.phpfreaks.com/topic/46274-442-vs-444/#findComment-225147 Share on other sites More sharing options...
utexas_pjm Posted April 9, 2007 Share Posted April 9, 2007 I don't think my first post was clear. You have a syntax problem everywhere you have used the $this pseudo variable. When referencing member data (that is, variables which belong to the class instance) you do so like: $this->varName (Note that there is only one $). That is why you are getting the notices. The code will still be interpreted and run, albeit with unpredictable results, the way you have it. When you use this: $this->$db = "foo"; PHP stores the value "foo" in a member variable with the name contained in the variable $db, in this case 'null'. Your code should look like this: <?php include 'database.class.php'; class MessageBoard { var $db; var $tz_offset; var $cur_user_id; function MessageBoard ($current_user_id, $type, $host, $database, $user, $pass) { $this->db = new database($type, $host, $database, $user, $pass); // line 11 $this->db->debug_mode(); // line 12 $this->db->query("SELECT timezone FROM fcms_users WHERE id = $current_user_id"); // line 13 $row = $this->db->get_row(); // line 14 switch($row['timezone']) { case 'EDT': $this->tz_offset = '-4 hours'; break; case 'CDT': $this->tz_offset = '-5 hours'; break; case 'MDT': $this->tz_offset = '-6 hours'; break; case 'PDT': $this->tz_offset = '-7 hours'; break; case 'AKDT': $this->tz_offset = '-8 hours'; break; case 'HST': $this->tz_offset = '-9 hours'; break; default: $this->tz_offset = '-4 hours'; break; } } ?> Hope this helps, Patrick Quote Link to comment https://forums.phpfreaks.com/topic/46274-442-vs-444/#findComment-225240 Share on other sites More sharing options...
ryanhowdy Posted April 11, 2007 Author Share Posted April 11, 2007 yes, that helps and sorry it took so long to reply. thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/46274-442-vs-444/#findComment-227171 Share on other sites More sharing options...
Daniel0 Posted April 17, 2007 Share Posted April 17, 2007 This is a result of trying to use an undefined variable (quite self-explanatory though). It could occur in the following examples: <?php $i++; // $i was not previously defined, but will still be defined as 1 now. Gives an E_NOTICE //... $names[] = "Daniel"; // $names was not defined, but it will be defined as an array with one key holding the value 'Daniel' now. Gives an E_NOTICE as well ?> Either ignore the errors and turn E_NOTICEs of or try to fix them. Quote Link to comment https://forums.phpfreaks.com/topic/46274-442-vs-444/#findComment-231579 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.