Bug #76385 [Opn->Ver]: Value of PHP_INT_MIN cannot be used as default value for int parameter
| From: | requinix@php.net | Date: | Tue, 29 May 2018 07:21:49 +0000 |
| Subject: | Bug #76385 [Opn->Ver]: Value of PHP_INT_MIN cannot be used as default value for int parameter | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-215405@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=76385&edit=1
ID: 76385
Updated by: requinix@php.net
Reported by: sebastian@php.net
Summary: Value of PHP_INT_MIN cannot be used as default value
for int parameter
-Status: Open
+Status: Verified
Type: Bug
Package: Scripting Engine problem
Operating System: Irrelevant
PHP Version: 7.2.6
Block user comment: N
Private report: N
New Comment:
This bug is hilarious:
The problem is that PHP parses "-9223372036854775808" not as a negative number but as a
"-" followed by "9223372036854775808". The latter value is, of course, just
barely too large for an integer so it overflows to a float.
Previous Comments:
------------------------------------------------------------------------
[2018-05-29 06:57:25] sebastian@php.net
It appears I stripped down the problem I am faced with too much for this bug report. I am sorry
about that.
I came across this in the context of https://github.com/sebastianbergmann/phpunit-mock-objects/issues/419.
More specifically, https://github.com/sebastianbergmann/phpunit-mock-objects/issues/419#issuecomment-392662972.
When function f(int $i = PHP_INT_MIN) {} is introspected using the Reflection API, getDefaultValue()
does not return "PHP_INT_MIN" for $i but rather the value of that constant.
And while I think that the Reflection API should be extended to optionally return the
"real" default value as it stands in the code, I still believe that function f(int $i =
-9223372036854775808) {} should work.
------------------------------------------------------------------------
[2018-05-29 06:44:09] gooh@php.net
The value of PHP_INT_MIN (but not of PHP_INT_MAX) is a float apparently.
You can use
function f(int $i = PHP_INT_MIN) {}
as a workaround.
------------------------------------------------------------------------
[2018-05-29 06:26:16] sebastian@php.net
It appears that the value of PHP_INT_MIN is generally not recognized as being an integer value:
$ cat a.php
<?php
var_dump(PHP_INT_MIN);
var_dump(is_int(-9223372036854775808));
$ php -n a.php
int(-9223372036854775808)
bool(false)
------------------------------------------------------------------------
[2018-05-29 06:16:24] sebastian@php.net
Description:
------------
$ cat a.php
<?php
var_dump(PHP_INT_MIN);
$ cat b.php
<?php
function f(int $i = -9223372036854775808)
{
}
$ php -nv
PHP 7.2.6 (cli) (built: May 22 2018 16:22:08) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
$ php -n a.php
int(-9223372036854775808)
$ php -n b.php
Fatal error: Default value for parameters with a integer type can only be integer or NULL in
/home/sb/b.php on line 2
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=76385&edit=1