Skip to content

Typed property DOMDocument::$documentElement must not be accessed before initialization #12215

Closed
@TimWolla

Description

@TimWolla

Description

The following code:

<?php

try {
	$xml = new \DOMDocument('1.0', 'UTF-8');
	$xml->loadXML(
		<<<'EOT'
		<?xml version="1.0" encoding="UTF-8"?>
		<dummy>
		</dummy>
		EOT
	);
	var_dump($xml->documentElement);
} catch (Error $e) {
	echo '<pre>'.$e.'</pre>';
}

Resulted in this output:

Error: Typed property DOMDocument::$documentElement must not be accessed before initialization in /var/www/html/xml.php:12
Stack trace:
#0 {main}

But I expected this output instead:

object(DOMElement)#2 (23) { ["schemaTypeInfo"]=> NULL ["tagName"]=> string(5) "dummy" ["firstElementChild"]=> NULL ["lastElementChild"]=> NULL ["childElementCount"]=> int(0) ["previousElementSibling"]=> NULL ["nextElementSibling"]=> NULL ["nodeName"]=> string(5) "dummy" ["nodeValue"]=> string(1) " " ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> NULL ["nextSibling"]=> NULL ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(5) "dummy" ["baseURI"]=> string(14) "/var/www/html/" ["textContent"]=> string(1) " " }

Exact reproduction steps on a fresh Ubuntu 22.04 VM:

  1. apt install php8.1 php-xml
  2. Open the script in the browser: It works fine.
  3. sed -i '/Dynamic Extensions/a extension=dom.so' /etc/php/8.1/apache2/php.ini (i.e. adding extension=dom.so to the dynamic extensions section of php.ini).
  4. systemctl restart apache2
  5. Open the script in the browser: It emits the error.

Note: The cause of the issue is a configuration error on the users end, but the error message is very non-descript and thus hard to debug. This issue is intended to ask for an improvement in error reporting (e.g. emitting an error because the extension is laoded twice or because a necessary dependency is not loaded in the right order).

PHP Version

PHP 8.1.2

Operating System

Ubuntu 22.04

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions