Re: Multi-Method Dispatch in PHP 5.1?

From: Date: Tue, 20 Apr 2004 18:57:31 +0000
Subject: Re: Multi-Method Dispatch in PHP 5.1?
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-9434@lists.php.net to get a copy of this message
On Apr 20, 2004, at 2:51 PM, Andrey Hristov wrote:
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); ?>
The problem with this is that that integer may well be a string when you look at it. There's no good way to do this past discriminating between scalar, array and object (by type, of course). I personally think that that is fine (adding an array typehint would be nice). George

« previous php.internals (#9434) next »