Here's something that I've been thinking about for some time. I want to chain together a set of methods like in the below shown example.
The concept of method chaining is no brainer, but what I want is to make all of our animals be added through the same add
method, so how should I figure out what type of animal that we're adding inside the add
method?
$zoo = new Zoo;
$lion = $zoo->lion->add('Lucas the lion');
$cockatoo = $zoo->cockatoo->add('Chris the cockatoo');
class Zoo {
function add($name) {
//How to figure out if the animal is a Lion or an Cockatoo?
}
}
$zoo->add(new Lion('Lucas'));
? Whereclass Lion implements IAnimal {}
$zoo->add(new Lion('Lucas the Lion'));
It decouples the Lion class from the Zoo class, and allows you to be far more flexible...if($item instanceof Lion){ /* do something with lions */ }
$item
originate?