peachpiecompiler / peachpie

PeachPie - the PHP compiler and runtime for .NET and .NET Core
https://www.peachpie.io
Apache License 2.0
2.33k stars 202 forks source link

Error in constructor of class inheriting DOMDocument? #1132

Closed wujwmail closed 5 months ago

wujwmail commented 6 months ago

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?

jakubmisek commented 5 months ago

Do you have a full call stack?

wujwmail commented 5 months ago

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

wujwmail commented 5 months ago

PhpTest.zip

wujwmail commented 5 months ago

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 commented 5 months ago

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); 
jakubmisek commented 5 months ago

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.