Hello everybody!

I know that method overloading was discussed on past and because a lot of
reasons it was declined [1]. I think that it should be considered maybe for
PHP 8, but for now I would like to suggest a different implementation.

Because of a stronger support to parameter types that PHP has now, I think
that is possible make this process more faster than before by handling it
only in case of exceptions. I wil explain better.

The biggest problem that I see about this topic is the loss of performance,
but I think that it could be avoided by create a new keyword "overload"
that should be applied before methods that could be overloaded. For
instance:

overload function sum(int $a, int $b): int;
overload function sum(float $b, float $b): float;

In this case, when call a method PHP could identify if it have a overloaded
implementation, so ONLY in this case it will reduce performace to decided
which implementation it should call.

I don't know how PHP could treats it internally, but when overload keyword
is identified, I think that it could generates a hash like
"sum/int,int/int" and "sum/float,float/float" (method name, parameters
types, return type). I don't know. So when method is called, it should
convert the argument types and create a similar hash, making possible to
identify what overloaded method should be run. It seems slow, but I think
that the opcache could simplify the next operations to connect directly the
call to the correct overloaded method.

Please, tell what do you think, because I really wants method overload on
PHP 8 or before. Currently we should do some code hacks to make it possible
that certainly is slower than a native implementation.

Thanks!

 [1] https://marc.info/?l=php-internals&m=121397217528280&w=2

-- 
David Rodrigues

Reply via email to