php-src/ext/json/tests/json_encode_pretty_print2.phpt
Nikita Popov 902d64390e Deprecate implicit dynamic properties
Writing to a proprety that hasn't been declared is deprecated,
unless the class uses the #[AllowDynamicProperties] attribute or
defines __get()/__set().

RFC: https://wiki.php.net/rfc/deprecate_dynamic_properties
2021-11-26 14:10:11 +01:00

56 lines
1.1 KiB
PHP

--TEST--
json_encode() with JSON_PRETTY_PRINT on declared properties
--FILE--
<?php
#[AllowDynamicProperties]
class MyClass {
public $x;
public $y;
public function __construct($x = 123, $y = []) {
$this->x = $x;
$this->y = $y;
}
}
class HasNoProperties {}
echo json_encode(new HasNoProperties()), "\n";
echo json_encode(new HasNoProperties(), JSON_PRETTY_PRINT), "\n";
echo json_encode(new MyClass()), "\n";
echo json_encode(new MyClass(), JSON_PRETTY_PRINT), "\n";
$obj = new MyClass();
$obj->dynamic = new MyClass(null, []);
echo json_encode($obj), "\n";
echo json_encode($obj, JSON_PRETTY_PRINT), "\n";
$obj = new MyClass();
unset($obj->y);
echo json_encode($obj), "\n";
echo json_encode($obj, JSON_PRETTY_PRINT), "\n";
unset($obj->x);
echo json_encode($obj), "\n";
echo json_encode($obj, JSON_PRETTY_PRINT), "\n";
?>
--EXPECT--
{}
{}
{"x":123,"y":[]}
{
"x": 123,
"y": []
}
{"x":123,"y":[],"dynamic":{"x":null,"y":[]}}
{
"x": 123,
"y": [],
"dynamic": {
"x": null,
"y": []
}
}
{"x":123}
{
"x": 123
}
{}
{}