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/
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
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
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
Maybe we should take that off list though and present just
the result of our research :-)
Yes please ;)
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