Estou com um problema ao localizar a interface no Repository de aluno, ele simplesmente não encontra. Já verifiquei os namespaces e a estrutura de pastas, porém o erro continua ocorrendo ao tentar rodar o Repository para verificar se roda sem erros. Abaixo deixo as informações:
StudentRepository.php
<?php
namespace Alura\Pdo\Domain\Repository;
use Alura\Pdo\Domain\Model\Student;
interface StudentRepository
{
public function allStudents(): array;
public function studentsBirthAt(\DateTimeInterface $birthDate): array;
public function save(Student $student): bool;
public function remove(Student $student): bool;
}
PdoStudentRepository.php
<?php
namespace Alura\Pdo\Domain\Repository;
use Alura\Pdo\Domain\Repository\StudentRepository;
use Alura\Pdo\Domain\Model\Student;
use Alura\Pdo\Infrastructure\Persistence\ConnectionCreator;
use DateTimeImmutable;
use DateTimeInterface;
use PDO;
use PDOStatement;
class PdoStudentRepository implements StudentRepository {
private $connection;
public function __construct()
{
$this->connection = ConnectionCreator::createConnection();
}
public function allStudents(): array {
$sqlSelect = 'SELECT * FROM students;';
$statement = $this->connection->prepare($sqlSelect);
$statement->execute();
return $this->hydrateStudents($statement);
}
public function studentsBirthAt(DateTimeInterface $birthDate): array {
$sqlSelect = 'SELECT * FROM students WHERE birth_date = :birth_date';
$statement = $this->connection->prepare($sqlSelect);
$statement->bindParam(':birth_date', $birthDate);
$statement->execute();
return $this->hydrateStudents($statement);
}
public function hydrateStudents(PDOStatement $statement): array {
$studentDataList = $statement->fetchAll(PDO::FETCH_ASSOC);
$studentList = [];
foreach($studentDataList as $student) {
$studentList[] = new Student($student['id'], $student['name'], new DateTimeImmutable($student['birth_date']));
}
return $studentList;
}
public function insert(Student $student): bool {
$sqlInsert = "INSERT INTO students(name, birth_date) VALUES(:name, :birth_date);";
$statement = $this->connection->prepare($sqlInsert);
$success = $statement->execute([
':name' => $student->name(),
':birth_date' => $student->birthDate()->format('Y-m-d')
]);
if($success) {
$student->defineId($this->connection->lastInsertId());
}
return $success;
}
public function update(Student $student): bool {
$sqlUpdate = "UPDATE students SET name = :name, :birth_date = :birth_date WHERE id = :id;";
$statement = $this->connection->prepare($sqlUpdate);
$statement->bindParam(':name', $student->name());
$statement->bindParam(':birth_date', $student->birthDate()->format('Y-m-d'));
$statement->bindParam(':id', $student->id(), PDO::PARAM_INT);
return $statement->execute();
}
public function save(Student $student): bool {
if ($student->id() === null) {
return $this->insert($student);
} else {
return $this->update($student);
}
}
public function remove(Student $student): bool {
$statement = $this->connection->prepare("DELETE FROM students WHERE id = :id");
$statement->bindParam(':id', $student->id(), PDO::PARAM_INT);
return $statement->execute();
}
}
Estrutura de pastas:
Erro:
PHP Fatal error: Uncaught Error: Interface "Alura\Pdo\Domain\Repository\StudentRepository" not found in C:\Users\Gabriel\Desktop\bancoPHP\src\Domain\Repository\Teste.php:7
Stack trace:
#0 {main}
thrown in C:\Users\Gabriel\Desktop\bancoPHP\src\Domain\Repository\Teste.php on line 7
Fatal error: Uncaught Error: Interface "Alura\Pdo\Domain\Repository\StudentRepository" not found in C:\Users\Gabriel\Desktop\bancoPHP\src\Domain\Repository\Teste.php:7
Stack trace:
#0 {main}
thrown in C:\Users\Gabriel\Desktop\bancoPHP\src\Domain\Repository\Teste.php on line 7
Obs: Esse Teste.php é uma reprodução do PdoStudentRepository.php pra fins de teste por causa do erro