|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2015-09-17 12:10 UTC] 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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 08 19:00:01 2025 UTC |
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 5and 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