mirror of
https://github.com/php/php-src.git
synced 2025-08-15 21:48:51 +02:00

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
56 lines
1.1 KiB
PHP
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
|
|
}
|
|
{}
|
|
{}
|