mirror of
https://github.com/php/php-src.git
synced 2025-08-18 06:58:55 +02:00

This makes the json encoding behavior the same as it was prior to the memory
optimizations added in f9f8c1c79c
(for objects with declared properties)
This is based on the code for the unoptimized case below the changes.
Buggy output prior to this commit:
```
{
"prop":"value"}
```
Correct output:
```
{
"prop": "value"
}
```
Closes GH-6811
55 lines
No EOL
1 KiB
PHP
55 lines
No EOL
1 KiB
PHP
--TEST--
|
|
json_encode() with JSON_PRETTY_PRINT on declared properties
|
|
--FILE--
|
|
<?php
|
|
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
|
|
}
|
|
{}
|
|
{}
|