Jump to content

4.4.2 vs 4.4.4


ryanhowdy

Recommended Posts

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;
        }
    } 

Link to comment
https://forums.phpfreaks.com/topic/46274-442-vs-444/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/46274-442-vs-444/#findComment-225240
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/46274-442-vs-444/#findComment-231579
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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