Post · 1 min read· p. 121

PHP quirk

PHP is very funny language. Here we are a simple class, without a constructor:

text
class Foo
{
}

$foo = new Foo($random, $names, $are, $not, $detected);
echo "ok!\n";

One can assume that interpreter will detect undeclared variables, but as their names state this doesn't happen (PHP versions 5.3..5.5):

text
$ php foo1.php
ok!

When the class Foo have the constructor:

text
class Foo
{
        public function __construct() { }
}

$foo = new Foo($random, $names, $are, $not, $detected);

everything works as expected:

text
$ php foo2.php
PHP Notice:  Undefined variable: random in /home/wojtek/foo2.php on line 10
PHP Notice:  Undefined variable: names in /home/wojtek/foo2.php on line 10
PHP Notice:  Undefined variable: are in /home/wojtek/foo2.php on line 10
PHP Notice:  Undefined variable: not in /home/wojtek/foo2.php on line 10
PHP Notice:  Undefined variable: detected in /home/wojtek/foo2.php on line 10