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
Share on other sites

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).

Link to comment
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
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
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.