0

I'm new to PHP and I'm following an online course (for this reason I'm not using autoloading or any other way to load classes).

My problem is that my controller class (in which I'm using "use") cannot see my model class (in which I've declared the namespace).

The error is:

Fatal error: Uncaught Error: Class 'App\Models\Post' not found in C:\xampp_php729\htdocs\Freeblog\App\Controllers\postController.php on line 20

Here is my code:

PostController.php


namespace App\Controllers;

use App\Models\Post;

class PostController {

    protected $layout = __DIR__ . '/../../Layout/index.template.php';
    protected $conn;
    public $content;
    public $posts;
    protected $myPost;

    public function __construct(\PDO $conn) {
        $this->conn = $conn;

        try {

            $this->myPost = new Post($conn);
        } catch (Exception $e) {
            die($e->getMessage());
        }
        $this->content = $this->process();
    }
....
} 

Post.php:

namespace App\Models;
use \PDO;


class Post {

    protected $conn;

    public function __construct(PDO $conn){
        $this->conn = $conn;

    } 
...
}

This is the project structure:

Project structure

3
  • 2
    So how do you expect to file to be included (loaded) if you don't use any autoloading? Commented Sep 29, 2019 at 9:15
  • I don't know I'm attending a course to learn it :) Commented Sep 29, 2019 at 9:25
  • In other two php files I've done the same thing and it worked.. the only difference is that they are in the same package (but I've tried to move Post and PostController in the same package and it doesn't work) Commented Sep 29, 2019 at 9:26

1 Answer 1

0

As you are not using autoloading, files in directory other than Controllers will not load.

So if you do not want to use autoloading, you should either use include statement or require statement to include the files manually(files which are in other directory).(Include file Post.php in PostController.php file)

As mentioned in your comments, other files which are working must be getting loaded because they exist in the same directory(same package does not matters here but same directory matters for other files to get loaded).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much! I was missing the "require" part in my index.php. Unfortunately I was focusing on this two files, not looking in the others :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.