[php-src] master: Merge branch 'PHP-8.6'
| From: | Ilia Alshanetsky | Date: | Fri, 20 Nov 2026 00:06:48 +0000 |
| Subject: | [php-src] master: Merge branch 'PHP-8.6' | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-143950@lists.php.net to get a copy of this message | ||
Author: Ilia Alshanetsky (iliaal)
Date: 2026-11-19T19:02:28-05:00
Commit: https://github.com/php/php-src/commit/a5a3db2ec94e780a7e849f82097cf6c5d85967aa
Raw diff: https://github.com/php/php-src/commit/a5a3db2ec94e780a7e849f82097cf6c5d85967aa.diff
Merge branch 'PHP-8.6'
* PHP-8.6:
ext/dom: Resolve constants of a node class when registering it
Changed paths:
A ext/dom/tests/registerNodeClass_undefined_constant.phpt
M ext/dom/document.c
Diff:
diff --git a/ext/dom/document.c b/ext/dom/document.c
index ccb2c08bc815..427a8f5dc08a 100644
--- a/ext/dom/document.c
+++ b/ext/dom/document.c
@@ -2303,9 +2303,14 @@ static void dom_document_register_node_class(INTERNAL_FUNCTION_PARAMETERS,
bool
}
if (ce == NULL || instanceof_function(ce, basece)) {
- if (UNEXPECTED(ce != NULL && (ce->ce_flags & ZEND_ACC_ABSTRACT))) {
- zend_argument_value_error(2, "must not be an abstract class");
- RETURN_THROWS();
+ if (ce != NULL) {
+ if (UNEXPECTED(ce->ce_flags & ZEND_ACC_ABSTRACT)) {
+ zend_argument_value_error(2, "must not be an abstract class");
+ RETURN_THROWS();
+ }
+ if (UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) {
+ RETURN_THROWS();
+ }
}
DOM_GET_THIS_INTERN(intern);
dom_set_doc_classmap(intern->document, basece, ce);
diff --git a/ext/dom/tests/registerNodeClass_undefined_constant.phpt
b/ext/dom/tests/registerNodeClass_undefined_constant.phpt
new file mode 100644
index 000000000000..f6809932a110
--- /dev/null
+++ b/ext/dom/tests/registerNodeClass_undefined_constant.phpt
@@ -0,0 +1,75 @@
+--TEST--
+registerNodeClass() with a class whose constant expressions cannot be evaluated
+--EXTENSIONS--
+dom
+--FILE--
+<?php
+
+const STRING_CONSTANT = "abc";
+
+class MyElement extends DOMElement {
+ public $x = UNDEFINED_CONSTANT;
+}
+
+class MyTypedElement extends DOMElement {
+ public int $x = STRING_CONSTANT;
+}
+
+class MyText extends DOMText {
+ const C = UNDEFINED_CONSTANT;
+}
+
+class MyDocument extends DOMDocument {
+ public static $x = UNDEFINED_CONSTANT;
+}
+
+class MyModernElement extends Dom\Element {
+ public $x = UNDEFINED_CONSTANT;
+}
+
+function register($doc, string $base, string $class) {
+ try {
+ $doc->registerNodeClass($base, $class);
+ echo "$class: registered\n";
+ } catch (Error $e) {
+ echo "$class: ", $e::class, ": ", $e->getMessage(), "\n";
+ }
+}
+
+$doc = new DOMDocument;
+$doc->loadXML('<root>text<a/></root>');
+register($doc, 'DOMElement', 'MyElement');
+register($doc, 'DOMElement', 'MyTypedElement');
+register($doc, 'DOMText', 'MyText');
+register($doc, 'DOMDocument', 'MyDocument');
+$root = $doc->documentElement;
+unset($doc);
+var_dump(get_class($root));
+var_dump(get_class($root->firstChild));
+var_dump(get_class($root->ownerDocument));
+
+$modern = Dom\XMLDocument::createFromString('<root><a/></root>');
+register($modern, 'Dom\Element', 'MyModernElement');
+var_dump(get_class($modern->documentElement));
+
+define('UNDEFINED_CONSTANT', 1);
+register($root->ownerDocument, 'DOMElement', 'MyElement');
+register($modern, 'Dom\Element', 'MyModernElement');
+var_dump(get_class($root->lastChild));
+var_dump(get_class($modern->documentElement->firstChild));
+
+?>
+--EXPECT--
+MyElement: Error: Undefined constant "UNDEFINED_CONSTANT"
+MyTypedElement: TypeError: Cannot assign string to property MyTypedElement::$x of type int
+MyText: Error: Undefined constant "UNDEFINED_CONSTANT"
+MyDocument: Error: Undefined constant "UNDEFINED_CONSTANT"
+string(10) "DOMElement"
+string(7) "DOMText"
+string(11) "DOMDocument"
+MyModernElement: Error: Undefined constant "UNDEFINED_CONSTANT"
+string(11) "Dom\Element"
+MyElement: registered
+MyModernElement: registered
+string(9) "MyElement"
+string(15) "MyModernElement"