php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70519 strict_types=0 does not work with NULL
Submitted: 2015-09-17 12:10 UTC Modified: 2015-09-17 12:39 UTC
From: vincent dot guth at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.0.0RC3 OS: Linux
Private report: No CVE-ID: None
 [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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-09-17 12:39 UTC] bwoebi@php.net
-Status: Open +Status: Not a bug
 [2015-09-17 12:39 UTC] 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
 [2017-02-10 13:47 UTC] 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 13:56 UTC] spam2 at rhsoft dot net
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
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 11:01:30 2024 UTC