RFC: Change GMP bool cast behavior (#15151)

Implementation of "RFC: Change GMP bool cast behavior"

https://wiki.php.net/rfc/fix_up_bcmath_number_class

Co-authored-by: Tim Düsterhus <tim@bastelstu.be>
This commit is contained in:
Saki Takamachi 2024-07-31 00:41:49 +09:00 committed by GitHub
parent 0e33b8dad9
commit 60afeb5537
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 25 additions and 2 deletions

2
NEWS
View file

@ -2,6 +2,8 @@ PHP NEWS
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
?? ??? ????, PHP 8.4.0beta1
- GMP:
. RFC: Change GMP bool cast behavior. (Saki Takamachi)
01 Aug 2024, PHP 8.4.0alpha3

View file

@ -51,6 +51,9 @@ PHP 8.4 UPGRADE NOTES
- GMP:
. The GMP class is now final and cannot be extended anymore.
RFC: https://wiki.php.net/rfc/gmp-final
. Casting a GMP object to bool changed so that 0 becomes false and everything else
becomes true.
RFC: https://wiki.php.net/rfc/fix_up_bcmath_number_class
- Intl:
. resourcebundle_get(), ResourceBundle::get(), and accessing offsets on a

View file

@ -298,6 +298,10 @@ static zend_result gmp_cast_object(zend_object *readobj, zval *writeobj, int typ
ZVAL_DOUBLE(writeobj, mpz_get_d(gmpnum));
}
return SUCCESS;
case _IS_BOOL:
gmpnum = GET_GMP_OBJECT_FROM_OBJ(readobj)->num;
ZVAL_BOOL(writeobj, mpz_sgn(gmpnum) != 0);
return SUCCESS;
default:
return FAILURE;
}

View file

@ -12,11 +12,25 @@ var_dump((int) $n);
var_dump((float) $n);
var_dump((bool) $n);
echo "\n";
$zero = gmp_init(0);
echo $zero, "\n";
var_dump((string) $zero);
var_dump((int) $zero);
var_dump((float) $zero);
var_dump((bool) $zero);
?>
--EXPECTF--
--EXPECT--
42
string(2) "42"
int(42)
float(42)
bool(true)
Recoverable fatal error: Object of class GMP could not be converted to bool in %s on line %d
0
string(1) "0"
int(0)
float(0)
bool(false)