# Multi-Method Dispatch in PHP 5.1?

- Thread: https://externals.io/message/9422
- Original archive: https://news-web.php.net/php.internals/9422
- Started by Sebastian Bergmann on 2004-04-20T14:50:11+00:00
- Messages: 6
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #9422 — Sebastian Bergmann — 2004-04-20T14:50:11+00:00

URL: https://externals.io/message/9422#9422

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?

-- 
Sebastian Bergmann
http://sebastian-bergmann.de/                   http://phpOpenTracker.de/

Das Buch zu PHP 5: http://professionelle-softwareentwicklung-mit-php5.de/

---

## #9437 — Christian Schneider — 2004-04-20T20:55:18+00:00

URL: https://externals.io/message/9422#9437 · In reply to: #9422

Sebastian Bergmann wrote:
>   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.

Actually I think multi-method dispatching for PHP is A Bad Thing[tm].

Multi-method dispatching is necessary for statically checked, inflexible 
languages without default parameters like Java. PHP has other means of 
handling the most common problems this tries to solve and having two 
methods of the same name is more confusing than helping IMHO.

PINJ,
- Chris

---

## #9439 — Michael Walter — 2004-04-21T05:32:35+00:00

URL: https://externals.io/message/9422#9439 · In reply to: #9437

Christian Schneider wrote:
> Sebastian Bergmann wrote:
> 
> > […]
> 
> 
> Actually I think multi-method dispatching for PHP is A Bad Thing[tm].
> 
> Multi-method dispatching is necessary for statically checked, inflexible 
> languages without default parameters like Java.
That's probably why Common Lisp, a language way more dynamic than PHP, 
provides you with a complete implementation of multiple dispatch. How 
exactly do you think are default parameters related to the issue, anyway?

> PHP has other means of handling the most common problems this tries to solve and having two 
> methods of the same name is more confusing than helping IMHO.
I assume you are you aware of what dynamic dispatch is trying to solve 
-- could you give an example of the 'other means' you are referring to?

Cheers,
Michael

---

## #9443 — Christian Schneider — 2004-04-21T09:13:35+00:00

URL: https://externals.io/message/9422#9443 · In reply to: #9439

Michael Walter wrote:
> How exactly do you think are default parameters related to the issue, anyway?

Java uses multi-method dispatch to work around missing default values:
function foo($a, $some_flag) { ... }
function foo($a) { foo($a, false); }
instead of
function foo($a, $some_flag = false) { ... }

> I assume you are you aware of what dynamic dispatch is trying to solve 

Whatever example I'd give you'd probably say it's not the relevant one. 
But if you give me a use case for dynamic dispatch I show you how I'd do 
it in PHP. Maybe we should take that off list though and present just 
the result of our research :-)

- Chris

---

## #9444 — Wez Furlong — 2004-04-21T09:25:15+00:00

URL: https://externals.io/message/9422#9444 · In reply to: #9443

> Maybe we should take that off list though and present just 
> the result of our research :-)

Yes please ;)

---

## #9458 — Michael Walter — 2004-04-21T11:26:04+00:00

URL: https://externals.io/message/9422#9458 · In reply to: #9443

Christian Schneider wrote:
> Michael Walter wrote:
> 
> > […]
> 
> 
> Java uses multi-method dispatch to work around missing default values:
> function foo($a, $some_flag) { ... }
> function foo($a) { foo($a, false); }
> instead of
> function foo($a, $some_flag = false) { ... }
Well yeah, we were presumably talking about different things, hence the 
question :) What you are talking about is a certain kind of "compile 
time" polymorphism, where the appropriate method is chosen at compile time.

I was referring to "runtime" polymorphism, i.e. the _dispatch_ is done 
on not only on one type (as with the type of the $object in an 
$object->method() call), but on 2 types (as in multimethod($rect,$circle)).

I'm pretty sure that Sebastian was referring to the same mechanism, as 
his example would need dispatch on 2 types, too, in order to work.

Also, I think functions which are dispatched using multiple types might 
as well be global functions, because usually (?) there is no such thing 
as a "first class" and "second class" dispatchee (is that a word?). For 
instance, consider:

function collides(Rect $rect, Rect $circle) { ... }
function collides(Rect $rect, Circle $circle) { ... }

> 
> > […]
> Whatever example I'd give you'd probably say it's not the relevant one. 
Naw, I didn't want to piss you of, just check whether we were dealing 
with the same thing. :)

> But if you give me a use case for dynamic dispatch I show you how I'd do 
> it in PHP. Maybe we should take that off list though and present just 
> the result of our research :-)
Yeah, well, you will show me the visitor pattern, which is an inferior 
work-around for the lack of multiple dispatch (because it only works 
well for closed sets of classes, although having it work for an open set 
of classes like with dynamic dispatch on the $this 'pointer' is desirable).

For a typical example, you might want to check out the Gang of Four book 
(Visitor pattern, which simulates multiple dispatch for multiple = 2) or 
consider above's collides() example.

The main difference between the 'isinstance' solution someone on this 
list posted is that you do not have to _modify_ a big large collides() 
function, but you just keep adding the new cases.

This is like with inheritance -- you use inheritance and dynamic 
dispatch on the $this 'pointer' in order to avoid series of 'isinstance' 
checks.

Anyway, I'm not really sure whether dynamic dispatch is appropriate in 
PHP myself, but I felt like pointing out the differences between static 
and dynamic dispatch might be important. ;)

Cheers,
Michael
