Multi-Method Dispatch in PHP 5.1?

php.internals

Sebastian Bergmann

22 years ago
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/

Christian Schneider

22 years ago
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

Michael Walter

22 years ago
Christian Schneider wrote:
> 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.
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

Christian Schneider

22 years ago
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

Wez Furlong

22 years ago
> Maybe we should take that off list though and present just > the result of our research :-)
Yes please ;)

Michael Walter

22 years ago
Christian Schneider wrote:
> 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) { ... }
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) { ... }
> >> 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.
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

Andi Gutmans

22 years ago
I don't want to prolong this discussion but I agree with the people who are against multi-method dispatch. I don't think it makes much sense in PHP, and I think that in the few places where someone really feels they need it, it can be implemented in user-land. It's just going to complicate the language for a small minority of people who feel they need this feature. Andi

Jevon Wright

22 years ago
But at the same time, it would be nice to have a standardised multi-method syntax or code - or users might develop their own weird and wacky, inconsistent ways of implementing it. Perhaps the code submitted before could be included in the PHP documentation, telling users on how they could emulate this style of code? Jevon ----- Original Message ----- From: "Andi Gutmans" <andi@zend.com> To: "Christian Schneider" <cschneid@cschneid.com>; "Michael Walter" <cm@leetspeak.org> Cc: <internals@lists.php.net> Sent: Wednesday, April 21, 2004 9:55 PM Subject: Re: [PHP-DEV] Re: Multi-Method Dispatch in PHP 5.1?
> I don't want to prolong this discussion but I agree with the people who
are
> against multi-method dispatch. I don't think it makes much sense in PHP, > and I think that in the few places where someone really feels they need
it,

Jason Garber

22 years ago
If it makes any difference... I would say no way to multi method dispatch. PHP, in my understanding, was a loosely typed language, and this is straining against that very concept. In the rare case that one needs multi-method dispatch, they can use a format such as: class xyz { public function Foo($p1 = NULL, $p2 = NULL) { if($p1 instanceof Bar && is_numeric($p2)) { //do something or return $this->Foo_Bar_Int($p1, $p2); } elseif($p1 instanceof OtherBar && is_null($p2)) { //do something or return $this->Foo_Otherbar($p1); } } ... } ~Jason At 4/21/2004 07:32 AM +0200, Michael Walter wrote: