I'm still a novice when it comes to polymorphism so I hope I can phrase my question correctly.
Let's say I have tables in my Database - Dog, Cat and Mouse. I want to be able to call something like:
$animal = new Animal("dog", 12);
with the animal class being something like:
class Animal {
protected $id;
protected $legs;
protected $height;
function __construct($type, $id) {
if ($type == "dog") {
return new Dog($id);
}
}
}
class Dog extends Animal {
function __construct($id) {
$this->id = $id;
$this->legs = 4;
$this->height = 1;
}
}
This doesn't work but I want to be able to call a new Animal and pass in the specific animal and have it be returned. How can I design this? (I'm using PHP).