php - Encapsulation issues
33
I have a tiny issues.
I have 3 classes:
Class Animal {
public $dog;
function __construct() {
$this->dog = new Dog();
}
}
Class Dog {
public $scream;
function __construct() {
$this->scream = new Scream();
}
}
Class Scream {
public $scream;
function __construct() {
}
public haaa(){
return 'hello World';
}
}
I'm trying to gethaaa()
function.. with
$animal = new Animal();
$animal->haaa();
If the functionhaaa()
is into theDog
class.. it works fine.. Is it possible that we have a limit of deep encapsulation?
Thank you!
Answer
Solution:
Based on your example it would be:
$animal->dog->haaa();
However, it might be best to change the design so that
Dog extends Animal
:That's more semantic, since dogs belong to the animal "kingdom".