Skip to content

Commit 7837edd

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 7837edd

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

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: 15 additions & 1 deletion
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
?>
1625
--EXPECTF--
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)