peachpie icon indicating copy to clipboard operation
peachpie copied to clipboard

Error in constructor of class inheriting DOMDocument?

Open wujwmail opened this issue 1 year ago • 5 comments

Peachpie.Library.XmlDom test of php.

<?php
class Class1 extends DOMDocument{
  public function __construct($file){
      parent::__construct();
      if(file_exists($file)) $this->load($file);
  }
}
$xml_filename="./abc.xml";  //  The content of abc.xml is: <root>abc</root>
$obj1=new Class1($xml_filename); 

call this class error: Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object. In Peachpie, both file_exists and load will go wrong?

wujwmail avatar Mar 21 '24 01:03 wujwmail

Do you have a full call stack?

jakubmisek avatar Apr 20 '24 13:04 jakubmisek

There seems to be no context support in __construct() in this Class1 subclass?

wujwmail avatar Apr 22 '24 15:04 wujwmail

PhpTest.zip

wujwmail avatar Apr 23 '24 14:04 wujwmail

Source code file | https://github.com/peachpiecompiler/peachpie/blob/master/src/Peachpie.Library.XmlDom/DOMDocument.cs Constructor definition in the source file: Line 275:

   public DOMDocument(string version = null, string encoding = null)

Should it be like this ?:

   public DOMDocument(Context ctx, string version = null, string encoding = null)

wujwmail avatar Apr 23 '24 22:04 wujwmail

This problem can be solved in a php script, but it's not elegant!

<?php
    class xmldom extends DOMDocument{
      public static function get_instance($file){
             $o=new xmldom();
             if(file_exists($file)) $o->load($file);
             return $o;
      }
      public function __construct(){
          parent::__construct();
      }
    }
    $xml_filename="./abc.xml";  //  The content of abc.xml is: <root>abc</root>
    $obj1=xmldom::get_instance($xml_filename); 

wujwmail avatar Apr 26 '24 22:04 wujwmail

hi @wujwmail ! Thank you very much for reporting the issue.

It is a bug in our implementation of DOMDocument. I'll take a look how to fix it.

jakubmisek avatar May 02 '24 13:05 jakubmisek