Skip to content

Commit 6e468bb

Browse files
committed
Fix json_encode result on DOMDocument
According to https://www.php.net/manual/en/class.domdocument: When using json_encode() on a DOMDocument object the result will be that of encoding an empty object. But this was broken in 8.1. The output was `{"config": null}`. That's because the config property is defined with a default value of NULL, hence it was included. The other properties are not included because they don't have a default property, and nothing is ever written to their backing field. Hence, the JSON encoder excludes them. Similarly, `(array) $doc` would yield the same `config` key in the array. Closes GH-11840.
1 parent 4553258 commit 6e468bb

File tree

5 files changed

+17
-5
lines changed

5 files changed

+17
-5
lines changed

NEWS

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ PHP NEWS
1414
. Fix empty argument cases for DOMParentNode methods. (nielsdos)
1515
. Fixed bug GH-11791 (Wrong default value of DOMDocument::xmlStandalone).
1616
(nielsdos)
17+
. Fix json_encode result on DOMDocument. (nielsdos)
1718

1819
- FFI:
1920
. Fix leaking definitions when using FFI::cdef()->new(...). (ilutov)

ext/dom/php_dom.stub.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ class DOMDocument extends DOMNode implements DOMParentNode
433433
* @readonly
434434
* @deprecated
435435
*/
436-
public mixed $config = null;
436+
public mixed $config;
437437

438438
public bool $formatOutput;
439439

ext/dom/php_dom_arginfo.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: a62e383b05df81ea245a7993215fb8ff4e1c7f9d */
2+
* Stub hash: 20a0ff883af3bbf073d9c8bc8246646ffafe7818 */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_dom_import_simplexml, 0, 1, DOMElement, 0)
55
ZEND_ARG_TYPE_INFO(0, node, IS_OBJECT, 0)
@@ -1436,7 +1436,7 @@ static zend_class_entry *register_class_DOMDocument(zend_class_entry *class_entr
14361436
zend_string_release(property_documentURI_name);
14371437

14381438
zval property_config_default_value;
1439-
ZVAL_NULL(&property_config_default_value);
1439+
ZVAL_UNDEF(&property_config_default_value);
14401440
zend_string *property_config_name = zend_string_init("config", sizeof("config") - 1, 1);
14411441
zend_declare_typed_property(class_entry, property_config_name, &property_config_default_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_ANY));
14421442
zend_string_release(property_config_name);
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
JSON encoding a DOMDocument
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
$doc = new DOMDocument;
8+
echo json_encode($doc);
9+
?>
10+
--EXPECT--
11+
{}

ext/dom/tests/domobject_debug_handler.phpt

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ var_dump($d);
1616
?>
1717
--EXPECTF--
1818
object(DOMDocument)#1 (39) {
19-
["config"]=>
20-
NULL
2119
["dynamicProperty"]=>
2220
object(stdClass)#2 (0) {
2321
}
@@ -45,6 +43,8 @@ object(DOMDocument)#1 (39) {
4543
bool(true)
4644
["documentURI"]=>
4745
string(%d) %s
46+
["config"]=>
47+
NULL
4848
["formatOutput"]=>
4949
bool(false)
5050
["validateOnParse"]=>

0 commit comments

Comments
 (0)