hyperfonic
hyperfonic

Reputation: 13

PHP fatal error: " Class '...' not found in ... on line

i'm trying to build an API for school project, i think i have everything right, but for some reason, the code doesn't read the use lines.

use repository\PDOPersonRepository;
use repository\PDOEventRepository;
use view\PersonJsonView;
use view\EventJsonView;
use controller\PersonController;

$user = 'root';
$password = '';
$database = 'wp1';
$pdo = null;

try {
$pdo = new PDO("mysql:host=127.0.0.1;dbname=wp1",
    $user, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE,
    PDO::ERRMODE_EXCEPTION);

$personPDORepository = new PDOPersonRepository($pdo);
$personJsonView = new PersonJsonView();
$personController = new PersonController($personPDORepository, $personJsonView);
$eventPDORepository = new PDOEventRepository($pdo);
$eventJSonView = new EventJsonView();
$eventController = new EventJsonView($eventJSonView, $eventPDORepository);

Fatal error: Class 'repository\PDOPersonRepository' not found in C:\xampp\htdocs\Werkpakket1\app.php on line 22

phpstorm doesn't give errors in the code, so i don't really understand why it does that

Upvotes: 1

Views: 4092

Answers (1)

Code4R7
Code4R7

Reputation: 2958

The use lines include classes based on their namespace rather than their physical location. It is different from include or require. If the use keyword is to find the classes, the code of those classes has to be included first.

Another approach is to use a custom autoloader which could match namespaces with the directories of the classes, so that they are loaded automagically only when you need (use) them.

In other words, your code above needs to be prepended with something like:

inculde 'Person.php';

Where that file would contain something like:

namespace repository;
class PDOPersonRepository { ... }

You could use multiple namespaces in a single file, but that is not common practice.

Upvotes: 1

Related Questions