-4

Fatal error: Uncaught Error: Typed property Funcionario::$matricula must not be accessed before initialization in C:\xampp\htdocs\sistemas\oo\classes\funcionario.class.php:17

Stack trace:
#0 C:\xampp\htdocs\sistemas\oo\classes\funcionario.class.php(27): Funcionario->getMatricula()
#1 C:\xampp\htdocs\sistemas\oo\classes\pessoa.class.php(50): Funcionario->getPerfil()
#2 C:\xampp\htdocs\sistemas\oo\classes\funcionario.class.php(11): Pessoa->__construct('Jose da Silva', '123.456.789-10', '1974-03-16')
#3 C:\xampp\htdocs\sistemas\oo\index.php(7): Funcionario->__construct('Jose da Silva', '123.456.789-10', '1974-03-16', '369852147')
#4 {main} thrown in C:\xampp\htdocs\sistemas\oo\classes\funcionario.class.php on line 17

pessoa.class.php:

<?php

abstract class Pessoa 
{
    private string $nome;
    private string $cpf;
    private string $nascimento;

    public function setNome(string $nome) : void
    {
        $this->nome = $nome;
    }

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

    public function setCpf(string $cpf) : void
    {
        $this->cpf = $cpf;
    }

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

    public function setNascimento(string $nascimento) : void
    {
        $this->nascimento = $nascimento;
    }

    public function getNascimento() : string
    {
        $dt_nascimento = new Datetime($this->nascimento);
        return $dt_nascimento->format('d/m/Y');
    }

    public function getPerfil() : string
    {
        return "<b>Nome:</b> {$this->getNome()}<br> <b>CPF:</b> {$this->getCpf()}<br> <b>Nascimento:</b> {$this->getNascimento()}<br>";
    }

    public function __construct(string $nome, string $cpf, string $nascimento)
    {
        $this->setNome($nome);
        $this->setCpf($cpf);
        $this->setNascimento($nascimento);
        echo $this->getPerfil();
    }
}

funcionario.class.php:

<?php

require_once "classes\pessoa.class.php";

class Funcionario extends Pessoa
{
    private string $matricula;

    public function __construct(string $nome, string $cpf, string $nascimento, string $matricula)
    {
        parent::__construct($nome, $cpf, $nascimento);
        $this->setMatricula($matricula);
    }

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

    public function setMatricula(string $matricula) : void
    {
        $this->matricula = $matricula;
    }

    public function getPerfil() : string
    {
        return "<b>Nome:</b> {$this->getNome()}<br> <b>CPF:</b> {$this->getCpf()}<br> <b>Nascimento:</b> {$this->getNascimento()} <br> <b>Matrícula:</b> {$this->getMatricula()}";
    }
    
}

index.php:

<?php
declare(strict_types=1);

require_once "classes/pessoa.class.php";
require_once "classes/funcionario.class.php";

$funcionario = new Funcionario("Jose da Silva", "123.456.789-10", "1974-03-16", "369852147");

Some advice? Thank you.

Paul T.
  • 4,703
  • 11
  • 25
  • 29

1 Answers1

0

Instead of using $this->setMatricula($matricula); in the constructor of Funcionario, fix to $this->matricula = $matricula.

    public function __construct(string $nome, string $cpf, string $nascimento, string $matricula)
    {
        parent::__construct($nome, $cpf, $nascimento);
        $this->matricula = $matricula;
    }

Plus, you are calling a method in Pessoa constructor before the values used in it being initialized.

    public function __construct(string $nome, string $cpf, string $nascimento)
    {
        $this->setNome($nome);
        $this->setCpf($cpf);
        $this->setNascimento($nascimento);
        echo $this->getPerfil(); // remove this line
    }