Skip to content

Commit 00683fb

Browse files
committed
Implementation of "RFC: Change GMP bool cast behavior"
https://wiki.php.net/rfc/fix_up_bcmath_number_class
1 parent 7fd54f9 commit 00683fb

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ PHP NEWS
2323

2424
- GMP:
2525
. The GMP class is now final and cannot be extended anymore.
26+
. RFC: Change GMP bool cast behavior (Saki Takamachi)
2627

2728
- Intl:
2829
. Added SpoofChecker::setAllowedChars to set unicode chars ranges.

UPGRADING

Lines changed: 3 additions & 0 deletions
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://github.com/php/php-src/pull/15151
5457

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

ext/gmp/gmp.c

Lines changed: 4 additions & 0 deletions
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

Lines changed: 16 additions & 2 deletions
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)