Re: Multi-Method Dispatch in PHP 5.1?
| From: | Andrey Hristov | Date: | Tue, 20 Apr 2004 18:51:04 +0000 |
| Subject: | Re: Multi-Method Dispatch in PHP 5.1? | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-9433@lists.php.net to get a copy of this message | ||
Quoting Sebastian Bergmann <sebastian@php.net>:
> Since we introduce class type hints in PHP 5.0 I think it would be a
> good thing [tm] to add multi-method dispatch in PHP 5.1.
>
> What I mean by this would be to allow for the following:
>
> class Foo {
> public function doSomething(Foo $foo) {}
>
> public function doSomething(Bar $bar) {}
> }
>
> How complicated would this be to implement?
>
Hi,
yesterday while waiting at the UBahn (Metro) station I came to the idea such
thing can me made in userland. func_get_args() can help combined with
get_type() for simple types. if it's class than => use also the class name if
you want.
<?php
class fubar {
protected function doSomething_integer_integer_double($i1,$i2,$f) {
$a = func_get_args();var_dump(__METHOD__, $a);
}
protected function doSomething_integer_double($i,$f) {
$a = func_get_args();var_dump(__METHOD__, $a);
}
public function doSomething() {
$args = func_get_args();
$method_name = __FUNCTION__;
foreach ($args as $v) {
$method_name .= "_".gettype($v);
}
if (method_exists($this, $method_name)) {
return call_user_func_array(array($this, $method_name),
$args);
}
}
}//class fubar
$a = new fubar();
$a->doSomething(1, 2.0);
$a->doSomething(3,4, 5.0);
?>
Cheers,
Andrey