1
0
mirror of https://github.com/php/php-src.git synced 2026-03-26 17:22:15 +01:00
Files
archived-php-src/Zend/tests/enum/json_encode.phpt
Ilija Tovilo 269c8dac1d Implement enums
RFC: https://wiki.php.net/rfc/enumerations

Co-authored-by: Nikita Popov <nikita.ppv@gmail.com>

Closes GH-6489.
2021-03-17 19:08:03 +01:00

60 lines
1006 B
PHP

--TEST--
Enum in json_encode
--FILE--
<?php
enum Foo {
case Bar;
}
enum IntFoo: int {
case Bar = 0;
}
enum StringFoo: string {
case Bar = 'Bar';
}
enum CustomFoo implements JsonSerializable {
case Bar;
public function jsonSerialize() {
return 'Custom ' . $this->name;
}
}
function test($value) {
var_dump(json_encode($value));
echo json_last_error_msg() . "\n";
try {
var_dump(json_encode($value, JSON_THROW_ON_ERROR));
echo json_last_error_msg() . "\n";
} catch (Exception $e) {
echo get_class($e) . ': ' . $e->getMessage() . "\n";
}
}
test(Foo::Bar);
test(IntFoo::Bar);
test(StringFoo::Bar);
test(CustomFoo::Bar);
?>
--EXPECT--
bool(false)
Non-backed enums have no default serialization
JsonException: Non-backed enums have no default serialization
string(1) "0"
No error
string(1) "0"
No error
string(5) ""Bar""
No error
string(5) ""Bar""
No error
string(12) ""Custom Bar""
No error
string(12) ""Custom Bar""
No error