php-src/Zend/tests/enum/comparison.phpt
Ilija Tovilo 5a482a139c
Fix enum to bool comparison
The compiler compiles $value == true to ZEND_BOOL, which always returns true for
objects (with the default cast_object handler). However, when compared to a
statically unknown rhs $value == $true, the resulting opcode ZEND_IS_EQUAL would
call the objects compare handler.

The zend_objects_not_comparable() handler, which is installed for enums and
other internal classes, blanketly returns false. This does not match the
ZEND_BOOL semantics.

Object to boolean comparison is now handled directly in zend_compare(),
analogous to object to null comparison. It continuous to call the cast_object
handler, but guarantees consistent behavior across ZEND_BOOL and ZEND_IS_EQUAL.

Fixes GH-16954
Closes GH-17031
2024-12-09 17:49:01 +01:00

57 lines
777 B
PHP

--TEST--
Enum comparison
--FILE--
<?php
enum Foo {
case Bar;
case Baz;
}
$bar = Foo::Bar;
$baz = Foo::Baz;
var_dump($bar === $bar);
var_dump($bar == $bar);
var_dump($bar === $baz);
var_dump($bar == $baz);
var_dump($baz === $bar);
var_dump($baz == $bar);
var_dump($bar > $bar);
var_dump($bar < $bar);
var_dump($bar >= $bar);
var_dump($bar <= $bar);
var_dump($bar > $baz);
var_dump($bar < $baz);
var_dump($bar >= $baz);
var_dump($bar <= $baz);
var_dump($bar > true);
var_dump($bar < true);
var_dump($bar >= true);
var_dump($bar <= true);
?>
--EXPECT--
bool(true)
bool(true)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(true)
bool(true)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(true)
bool(true)