Welcome Guest, Not a member yet? Register   Sign In
Unable to instantiate DOMDocument object inside Model class
#1

(This post was last modified: 08-10-2023, 08:32 PM by mshahrim.)

Hello.
Below is my model. (please take a look at $dom)
PHP Code:
<?php namespace App\Models;

use 
CodeIgniter\Model;

class 
SomethingModel extends Model
{
    public function doSomething()
    {
        // do something
        $dom = new DOMDocument();
    }
}

?>

I cannot instantiate DOMDocument.

Error logs showed this:
CRITICAL - 2023-08-11 11:05:10 --> Class 'App\Models\DOMDocument' not found

As if it thinks I'm referencing a model.

But when I do the same on View files, it has no problem.

(08-10-2023, 08:14 PM)mshahrim Wrote: Hello.
Below is my model. (please take a look at $dom)
PHP Code:
<?php namespace App\Models;

use 
CodeIgniter\Model;

class 
SomethingModel extends Model
{
    public function doSomething()
    {
        // do something
        $dom = new DOMDocument();
    }
}

?>

I cannot instantiate DOMDocument.

Error logs showed this:
CRITICAL - 2023-08-11 11:05:10 --> Class 'App\Models\DOMDocument' not found

As if it thinks I'm referencing a model.

But when I do the same on View files, it has no problem.

Okay, I have found the solution.

Since I need to instantiate DOMDocument, the PHP built-in objects, all I need to do is just add this declaration:
PHP Code:
use DOMDocument
Reply
#2

Heh, its base syntax - new \DOMDocument or use DOMDocument
Reply
#3

Try putting a slash in front of it like below, if that does not work it could be that the html DOM is not installed
in your php version.

PHP Code:
$dom = new \DOMDocument(); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

As you've found out, add a use statement at the top of the file:use DOMDocument;
This allows you to instantiate the DOMDocument class without the leading backslash in the rest of your file: $dom = new DOMDocument();
Shortcode Κατασκευή Eshop + Δημιουργία Ιστοσελίδων 
Athens Greece 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB