3
My question is this: If I can declare a constructor in a class abstract
and also in the child classes. And also how to access their values. For example:
abstract class Animal {
private $nome;
private $idade;
public funtion __construct($nome, $idade) {
$this->nome = $nome;
$this->idade = $idade;
}
}
And in the daughter class, who inherits from the class Animal
:
class Cachorro extends Animal {
private $corPelo;
public function __construct($corPelo) {
$this->corPelo = $corPelo;
}
}
And when I try to instantiate I call the class Cachorro
instantiating the values for Animal
:
$dog = new Cachorro('belinha', 4, 'preto');
But that way I can’t access corPelo
daughter-class Cachorro
, only the values of the mother class Animal
.
I’m doing it wrong?
Have you removed the '' of the $dog = new Dog ('Little Bee',4,'black')? It would be 'Little Bee' and not 'Little Bee'. Or I’m wrong?
– Inkeliz
I typed wrong the code here in stackoverflow. But even though it’s like this 'Belinha' (between quotes) does not work :/
– Leandro Macedo
but in case I use a daughter-class to inherit the abstract mother class, it might ?
– Leandro Macedo
then I urge the child class and access the attributes of the abstract mother class
– Leandro Macedo
Forget it, I removed the comment. I saw the instance of the wrong class.
– Papa Charlie