Make strict mode more strict?

From: Date: Wed, 16 Sep 2015 22:10:55 +0000
Subject: Make strict mode more strict?
Groups: php.internals 
Request: Send a blank email to internals+get-88268@lists.php.net to get a copy of this message
Hi all, PHP 7 has strict_types mode for function parameters/return values and these are binded to certain type strictly. https://wiki.php.net/rfc/scalar_type_hints_v5 Why not make strict_types mode more strict? The idea is as follows: <?php declare(strict_types=1); function foo(int &$i) { $i = "string"; // Raise error $i = function_returns_string(); // Raise error $i = 1234.5678; // Raise error $i = function_returns_float(); // Raise error $n = 123; // do something $n = array(); // Raise error } ?> Assigning different type to already initialized variable is a bug most likely. There may be cases that variable should have several types, e.g. return INT for success and FALSE for failure, but programmers can use different variable or return value directly or make BOOL/NULL exception. This is useful with reference especially. For example, <?php declare(strict_types=1); function foo(int &$i) { $i = 'string'; } $i = 0; foo($i); var_dump($i); ?> outputs string(6) "string" Just an idea. Any comments? -- Yasuo Ohgaki yohgaki@ohgaki.net

« previous php.internals (#88268) next »