-1

I have a class named ModelFactory which is supposed to return an instance of Model object. But for some reason, PHP can't seem to find it.

Warning: require_once(../../../../../model.php): failed to open stream: No such file or directory

Here's my ModelFactory class which is loaded via DI for reference:

namespace jas\Pdf\Factory;

require_once "../../../../../model.php";

use Model;

class ModelFactory
{
    /**
     * @return Model
     */
    public function create(){
        return new Model(false);
    }
}

And here's my Model class, which is 5 levels above ModelFactory class, loaded via require_once:

class Model
{

    public function __construct($serverRequest = false)
    {
        if(!$serverRequest){
            $this->checkIfLocal();
        }
        $this->conn = $this->connectToDB();
    }
}

I'm kind of confused. Is the PHP throwing the error due to Model class not having a namespace, or is it because ModelFactory namespace conflicting with the file paths? Thank you so much for any insights you can provide.

Jas
  • 188
  • 1
  • 3
  • 16

1 Answers1

-1

I fixed it by referring to this question: PHP - Failed to open stream : No such file or directory

I used __DIR__ as the baseline of the path. Applying this, the path looks now like this:

namespace jas\Pdf\Factory;

require_once __DIR__."/../../../../../model.php";

use Model;

class ModelFactory
{
    /**
     * @return Model
     */
    public function create(){
        return new Model(false);
    }
}

And so, the class is now properly loaded.

Jas
  • 188
  • 1
  • 3
  • 16