1
How do I access the parent class attributes once I instated the child?
Direct access $Filho->atributoPai
doesn’t work.
Access by function $Filho->getterPai
doesn’t work.
For every inherited child I must make getters to your father’s attributes?
<?php
class Pessoa {
function __construct($nome, $sexo, $idade) {
$this->nome = $nome;
$this->sexo = $sexo;
$this->idade = $idade;
}
function getNome() {
return $this->nome;
}
private $nome;
private $sexo;
private $idade;
}
class Amigo extends Pessoa {
function __construct($nome, $sexo, $idade, $diaDoAniversario) {
parent::__construct($nome, $sexo, $idade);
$this->diaDoAniversario = $diaDoAniversario;
}
private $diaDoAniversario;
}
$joao = new Amigo("Jonis", "masc", 20, 30);
echo "Nome: $joao->nome"; // Notice: Undefined property: Amigo::$nome
echo "Nome: $joao->getNome()"; // Undefined property: Amigo::$getNome
?>
Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).
– Maniero