I don't know enough about how programming languages work to even know what this is called. But when working on an event emitter, I thought it'd be cool if PHP would be able to support this:
class FooClass {
function bam() { echo 'BAM!'; }
}
function foo(array $bar = []) {
print_r($bar);
}
function foo(array $bar = [], $baz) {
echo $baz . ' likes ' . $bar[0];
}
function foo(FooClass $bar) {
$bar->bam();
}
$data = ['foo','bar']
foo($data);
// prints results from `print_r($data);`
foo($data, 'david');
// david likes foo
$fooObject = new FooClass();
foo($fooObject);
// BAM!
And I'm trying to figure out how it'd work with closures.
$myFunc = function (FooClass $foo) {
};
$myFunc = function (array $foo) {
};
This would make, for example, coding a Listener()
super easy to write:
$listener = new Listener();
$listener->on(function (SuccessEvent $foo) {
// . . .
});
$listener->on(function (ErrorEvent $foo) {
// . . .
});
$listener->on(function (BarEvent $foo) {
// . . .
});
Or is all this just a bad practice idea? Thoughts? :)
A link from @ircmaxell: http://marc.info/?t=121388053200003&r=1&w=2