PHP quirk

Author:Wojciech Muła
Added on:2013-09-01

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

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

$ php foo1.php
ok!

When the class Foo have the constructor:

class Foo
{
        public function __construct() { }
}

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

everything works as expected:

$ 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