Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in constructor of class inheriting DOMDocument? #1132

Closed
wujwmail opened this issue Mar 21, 2024 · 6 comments
Closed

Error in constructor of class inheriting DOMDocument? #1132

wujwmail opened this issue Mar 21, 2024 · 6 comments

Comments

@wujwmail
Copy link

wujwmail commented Mar 21, 2024

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
Copy link
Member

Do you have a full call stack?

@wujwmail
Copy link
Author

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

@wujwmail
Copy link
Author

PhpTest.zip

@wujwmail
Copy link
Author

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
Copy link
Author

wujwmail commented Apr 26, 2024

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
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants