xref: /PHP-8.1/ext/json/tests/bug61978.phpt (revision 5beba0b0)
1--TEST--
2Bug #61978 (Object recursion not detected for classes that implement JsonSerializable)
3--FILE--
4<?php
5
6class JsonTest1 {
7    public $test;
8    public $me;
9    public function __construct() {
10        $this->test = '123';
11        $this->me  = $this;
12    }
13}
14
15class JsonTest2 implements JsonSerializable {
16    public $test;
17    public function __construct() {
18        $this->test = '123';
19    }
20    public function jsonSerialize(): mixed {
21        return array(
22            'test' => $this->test,
23            'me'   => $this
24        );
25    }
26}
27
28
29$obj1 = new JsonTest1();
30var_dump(json_encode($obj1, JSON_PARTIAL_OUTPUT_ON_ERROR));
31
32echo "==\n";
33
34$obj2 = new JsonTest2();
35var_dump(json_encode($obj2, JSON_PARTIAL_OUTPUT_ON_ERROR));
36
37?>
38--EXPECT--
39string(24) "{"test":"123","me":null}"
40==
41string(24) "{"test":"123","me":null}"
42