niedziela, 1 września 2013

PHP quirk

PHP is very funny language. Here we are simple class, without 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 class Foo have 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

Brak komentarzy: