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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
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-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 19:01:31 2024 UTC