Skip to content

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

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

Activity

  1. added 5 commits that reference this issue on Sep 15, 2023
    89e4184
    368e195
    da6097f
    84fae4a
    b08802d
  2. added 2 commits that reference this issue on Sep 20, 2023
    aab6bbc
    8a812c3
  3. vmajor commented on Oct 17, 2023

    @vmajor

    OK, I have no run into this error as well on my VM with Ubnutu 22.04.

    php -v
    PHP 8.1.12-1ubuntu4.3 (cli) (built: Aug 17 2023 17:37:48) (NTS)
    Copyright (c) The PHP Group
    Zend Engine v4.1.12, Copyright (c) Zend Technologies
        with Zend OPcache v8.1.12-1ubuntu4.3, Copyright (c), by Zend Technologies
    

    Code that triggers this error. I am not a php developer, just a user that is stuck with a presumably functioning plugin that fails to function on my brand new VM. This is a short code snippet that is designed to generate the error that crashes the plugin:

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

    Error:

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

    DOM section of the php.ini

    DOM/XML enabled
    DOM/XML API Version 20031129
    libxml Version 2.9.14
    HTML Support enabled
    XPath Support enabled
    XPointer Support enabled
    Schema Support enabled
    RelaxNG Support enabled

    I have no option to use a higher version of php because one is not available in the official lunar channel, and I cannot install ondrej/ppa because no repo is available for my version of ubuntu. Attempting to add the ppa fails with a 404.

    Help?

  4. ndossche commented on Oct 17, 2023

    @ndossche
    Member

    @vmajor you are somehow loading the DOM extension twice, causing it to get confused.
    Either the DOM extension is embedded in the php binary, and you don't need to load it in the php.ini again. In that case remove the extension line for DOM in php.ini.
    Or you are loading it twice in the php.ini and you have to remove the duplicate line.

    I have no experience with ppa.
    But in any case, you can always install a newer PHM version by compiling from the git repo yourself.

  5. vmajor commented on Oct 17, 2023

    @vmajor

    Oh... thank you for the extremely fast, and correct reply. I had only one instance of DOM in php.ini, but I commented it out and now the test script works. Next, testing the presumably functional plugin.

    Thank you so much.

  6. VictorZZZZ commented on May 7, 2025

    @VictorZZZZ

    @vmajor you are somehow loading the DOM extension twice, causing it to get confused. Either the DOM extension is embedded in the php binary, and you don't need to load it in the php.ini again. In that case remove the extension line for DOM in php.ini. Or you are loading it twice in the php.ini and you have to remove the duplicate line.

    I have no experience with ppa. But in any case, you can always install a newer PHM version by compiling from the git repo yourself.

    Also managed to me. Thank's a lot.
    Just removed from my php.ini file

    ;extension=dom

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions