php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76385 Value of PHP_INT_MIN cannot be used as default value for int parameter
Submitted: 2018-05-29 06:16 UTC Modified: 2018-05-29 11:25 UTC
Votes:3
Avg. Score:3.0 ± 0.8
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:2 (66.7%)
From: sebastian@php.net Assigned: cmb (profile)
Status: Duplicate Package: Scripting Engine problem
PHP Version: 7.2.6 OS: Irrelevant
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: sebastian@php.net
New email:
PHP Version: OS:

 

 [2018-05-29 06:16 UTC] 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


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-05-29 06:26 UTC] 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:44 UTC] 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:57 UTC] 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 07:21 UTC] requinix@php.net
-Status: Open +Status: Verified
 [2018-05-29 07:21 UTC] requinix@php.net
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.
 [2018-05-29 11:25 UTC] cmb@php.net
-Status: Verified +Status: Duplicate -Assigned To: +Assigned To: cmb
 [2018-05-29 11:25 UTC] cmb@php.net
Actually, this is a duplicate of bug #53934.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Mon Jan 20 05:01:25 2020 UTC