3
respostas

Erro

Fatal error: Uncaught TypeError: Alura\Pdo\Domain\Model\Student::__construct(): Argument #2 ($name) must be of type string, null given, called in C:\Users\matheus\Desktop\prog\php\php pdo\student-list.php on line 18 and defined in C:\Users\matheus\Desktop\prog\php\php pdo\src\Domain\Model\Student.php:11 Stack trace:

#0 C:\Users\matheus\Desktop\prog\php\php pdo\student-list.php(18): Alura\Pdo\Domain\Model\Student->__construct(13, NULL, Object(DateTimeImmutable))

#1 {main} thrown in C:\Users\matheus\Desktop\prog\php\php pdo\src\Domain\Model\Student.php on line 11

Simplesmente não sei onde ta esse erro, já baixei os arquivos das aulas pra testar e ta tudo igual e continua esse erro

3 respostas

lista-alunos.php

<?php

use Alura\Pdo\Domain\Model\Student;

require_once 'vendor/autoload.php';

$databasePath = __DIR__ . '/banco.sqlite';
$pdo = new PDO('sqlite:' . $databasePath);

$statement = $pdo->query('SELECT * FROM students;');
$studentDataList = $statement->fetchAll(PDO::FETCH_ASSOC);
$studentList = [];

foreach ($studentDataList as $studentData) {
    $studentList[] = new Student(
        $studentData['id'],
        $studentData['name'],
        new \DateTimeImmutable($studentData['birth_date'])
    );
}

var_dump($studentList);

Student.php

<?php

namespace Alura\Pdo\Domain\Model;

class Student
{
    private ?int $id;
    private string $name;
    private \DateTimeInterface $birthDate;

    public function __construct(?int $id, string $name, \DateTimeInterface $birthDate)
    {
        $this->id = $id;
        $this->name = $name;
        $this->birthDate = $birthDate;
    }

    public function id(): ?int
    {
        return $this->id;
    }

    public function name(): string
    {
        return $this->name;
    }

    public function birthDate(): \DateTimeInterface
    {
        return $this->birthDate;
    }

    public function age(): int
    {
        return $this->birthDate
            ->diff(new \DateTimeImmutable())
            ->y;
    }
}

Provavelmente seu banco não está correto. A coluna name existe (com esse exato nome) e possui um valor pra todos os registros?

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software