0

I spent more than 10 hours to find out the typo for debugging my PHP program. I expected PHP would produce an error when using an undefined variable. But when it is used as an object in a method, it doesn't. Is there a reason for it?

<?php

    $oClass = new MyClass;
    // $oCless->tihs('key', 'taht');    //<-- triggers the error: Notice: Undefined variable
    $oClass->tihs('key', 'taht');
    echo $oClass->arr['key'];

    class MyClass {
        public $arr = array('key' => 'foo');

        function tihs($key, $value) {
            $tihs->arr[$key] = $value;  //<-- this does not cause an error message.
        }
    }
?>
Teno
  • 2,582
  • 4
  • 35
  • 57
  • the misspelling of 'this' is very confusing – Lucas Green Sep 05 '12 at 03:59
  • 1
    having a function named 'this' is also very confusing – Lucas Green Sep 05 '12 at 03:59
  • undefined values in PHP are not an error. they just produce a warning if you've got the right level of display_errors/error_reporting enabled. – Marc B Sep 05 '12 at 04:07
  • 1
    Make sure your error reporting includes notices, and set up a custom error handler that aborts all processing after any error is reported, whether it is a notice, warning or a real error. Then prepare to fix a lot of code which will suddenly start throwing notices. – DCoder Sep 05 '12 at 04:08

4 Answers4

1

It is because of PHP trickery...

Under the covers, PHP is actually creating an object called tihs, adding an array to the object called arr and setting key to value.

Here is a print_r($tihs); after the assignment:

stdClass Object
(
    [arr] => Array
        (
            [key] => taht
        )

)
sberry
  • 128,281
  • 18
  • 138
  • 165
1

Normally if the error reporting level is set to E_ALL | E_STRICT (or E_ALL as of PHP 5.4.0) it should spit out an E_STRICT error. For instance, this code:

error_reporting(E_ALL | E_STRICT);
$tihs->adrr = 453;  

Produces:

Strict Standards: Creating default object from empty value in [...]

Interestingly enough, if you specifically create an array instead of an ordinary variable as a property of an object that doesn't exist, e.g.:

error_reporting(E_ALL | E_STRICT);
$tihs->adrr[25] = 453;  

No strict standards error is shown! It looks like this could potentially be something PHP folks might want to fix, because I'm not aware this is documented nor I think there's a legitimate reason for this behaviour.

For the record, in both cases regardless of the error a new stdClass is being created on the fly instead, like sberry mentions in his answer.

Community
  • 1
  • 1
Mahn
  • 16,261
  • 16
  • 62
  • 78
0

You seemed to have misspelt $oClass as $oCless

daveh
  • 3,586
  • 1
  • 20
  • 13
0

Agreed, $oCless instead of $oClass would give you an undefined variable error.

Also, "this" is a keyword in most languages and may be in php as well. You should refrain from using it so that it doesn't come out in other languages as a habit. You'll get way more errors if you're using "this" as function and variable names. You wouldn't even get things to compile.

Cody
  • 168
  • 6