Bug #70519 [Com]: strict_types=0 does not work with NULL

From: Date: Fri, 10 Feb 2017 13:56:34 +0000
Subject: Bug #70519 [Com]: strict_types=0 does not work with NULL
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-207252@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=70519&edit=1 ID: 70519 Comment by: spam2 at rhsoft dot net Reported by: vincent dot guth at gmail dot com Summary: strict_types=0 does not work with NULL Status: Not a bug Type: Bug Package: *General Issues Operating System: Linux PHP Version: 7.0.0RC3 Block user comment: N Private report: N New Comment: and it defeats the whole purpose because before you just wrote function($x) { $x = (int)$x; } now with function(int $x) you must place the (int)-casting all around your code in the callers or check explicit if the value is NULL Previous Comments: ------------------------------------------------------------------------ [2017-02-10 13:47:38] spam2 at rhsoft dot net than this RFC is broken by design because it's completly inconsistent * native functions: that behavior happens only with declare(strict_types=1) * useland code: the behavior is ALWAYS like declare(strict_types=1) _______________________________________________________ OK [harry@srv-rhsoft:/downloads]$ cat test.php <?php $test = NULL; echo trim($test); ?> [harry@srv-rhsoft:/downloads]$ php test.php [harry@srv-rhsoft:/downloads]$ [harry@srv-rhsoft:/downloads]$ cat test.php <?php declare(strict_types=1); $test = NULL; echo trim($test); ?> [harry@srv-rhsoft:/downloads]$ php test.php Fatal error: Uncaught TypeError: trim() expects parameter 1 to be string, null given in /mnt/data/downloads/test.php:3 Stack trace: #0 /mnt/data/downloads/test.php(3): trim(NULL) #1 {main} thrown in /mnt/data/downloads/test.php on line 3 ________________________________________________________ COMPLETLY BROKEN BEHAVIOR: [harry@srv-rhsoft:/downloads]$ cat test.php <?php $test = NULL; echo test($test); function test(string $input) { } ?> [harry@srv-rhsoft:/downloads]$ php test.php Fatal error: Uncaught TypeError: Argument 1 passed to test() must be of the type string, null given, called in /mnt/data/downloads/test.php on line 3 and defined in /mnt/data/downloads/test.php:5 Stack trace: #0 /mnt/data/downloads/test.php(3): test(NULL) #1 {main} thrown in /mnt/data/downloads/test.php on line 5 ------------------------------------------------------------------------ [2017-02-10 12:56:45] nikic@php.net Related To: Bug #74075 ------------------------------------------------------------------------ [2015-09-17 12:39:02] bwoebi@php.net As defined by the RFC, this is totally intentional. See: https://wiki.php.net/rfc/scalar_type_hints_v5#behaviour_of_weak_type_checks ------------------------------------------------------------------------ [2015-09-17 12:10:55] vincent dot guth at gmail dot com Description: ------------ declare(strict_types=0); does not work with NULL value when using argument type declarations (see test script). Test script: --------------- php7 -r 'declare(strict_types=0); function test(string $s) {var_dump($s);} test(123);' php7 -r 'declare(strict_types=0); function test(string $s) {var_dump($s);} test(NULL);' Expected result: ---------------- string(3) "123" string(0) "" Actual result: -------------- string(3) "123" PHP Fatal error: Uncaught TypeError: Argument 1 passed to test() must be of the type string, null given, called in Command line code on line 1 and defined in Command line code:1 Stack trace: #0 Command line code(1): test(NULL) #1 {main} thrown in Command line code on line 1 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=70519&edit=1

« previous php.bugs (#207252) next »