Home > front end >  Uncaught Error: SimpleXMLElement is not properly initialized
Uncaught Error: SimpleXMLElement is not properly initialized

Time:10-29

Hello I m working on php migration from V 7.2 to 8.1 and I have an error with SimpleXMLElement :

$string = <<<XML
<a>
 <b>
  <c>text</c>
  <c>stuff</c>
 </b>
 <d>
  <c>code</c>
 </d>
</a>
XML;

$xml = new SimpleXMLElement($string);
unset($xml[0]);

print_r(array(
    $xml,
    !empty($xml)
));

In php 7.2 a warning is triggered but for php 8.1 I've this error

Fatal error: Uncaught Error: SimpleXMLElement is not properly initialized in

CodePudding user response:

I use unset to delete xml content, I found a solution it's catching php ERROR

$string = <<<XML
<a>
 <b>
  <c>text</c>
  <c>stuff</c>
 </b>
 <d>
  <c>code</c>
 </d>
</a>
XML;

$xml = new SimpleXMLElement($string);
unset($xml[0]);

try {
    print_r(array(
       $xml,
      !empty($xml)
    ));
}catch (\Error  $e) {
    trigger_error($e->getMessage(), E_USER_WARNING);
    // do someting
}
  • Related