Skip to content

Commit 60afeb5

Browse files
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 <[email protected]>
1 parent 0e33b8d commit 60afeb5

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

NEWS

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? ????, PHP 8.4.0beta1
44

5+
- GMP:
6+
. RFC: Change GMP bool cast behavior. (Saki Takamachi)
57

68
01 Aug 2024, PHP 8.4.0alpha3
79

UPGRADING

+3
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ PHP 8.4 UPGRADE NOTES
5151
- GMP:
5252
. The GMP class is now final and cannot be extended anymore.
5353
RFC: https://wiki.php.net/rfc/gmp-final
54+
. Casting a GMP object to bool changed so that 0 becomes false and everything else
55+
becomes true.
56+
RFC: https://wiki.php.net/rfc/fix_up_bcmath_number_class
5457

5558
- Intl:
5659
. resourcebundle_get(), ResourceBundle::get(), and accessing offsets on a

ext/gmp/gmp.c

+4
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,10 @@ static zend_result gmp_cast_object(zend_object *readobj, zval *writeobj, int typ
298298
ZVAL_DOUBLE(writeobj, mpz_get_d(gmpnum));
299299
}
300300
return SUCCESS;
301+
case _IS_BOOL:
302+
gmpnum = GET_GMP_OBJECT_FROM_OBJ(readobj)->num;
303+
ZVAL_BOOL(writeobj, mpz_sgn(gmpnum) != 0);
304+
return SUCCESS;
301305
default:
302306
return FAILURE;
303307
}

ext/gmp/tests/cast.phpt

+16-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,25 @@ var_dump((int) $n);
1212
var_dump((float) $n);
1313
var_dump((bool) $n);
1414

15+
echo "\n";
16+
17+
$zero = gmp_init(0);
18+
echo $zero, "\n";
19+
var_dump((string) $zero);
20+
var_dump((int) $zero);
21+
var_dump((float) $zero);
22+
var_dump((bool) $zero);
23+
1524
?>
16-
--EXPECTF--
25+
--EXPECT--
1726
42
1827
string(2) "42"
1928
int(42)
2029
float(42)
30+
bool(true)
2131

22-
Recoverable fatal error: Object of class GMP could not be converted to bool in %s on line %d
32+
0
33+
string(1) "0"
34+
int(0)
35+
float(0)
36+
bool(false)

0 commit comments

Comments
 (0)