Hi,

Others have already raised good technical points (performance, how to distinguish when we have dynamic/weak typing), but I'd like to offer another argument against it, that is, that even if it were possible we might not want overloading anyway.

From my perspective, function and method overloading in languages like C++ or C# is useful for mainly two reasons: allowing omitting unnecessary arguments (or, allowing providing additional, more specific arguments), and accepting multiple types for arguments.

However, we already have solutions for both of these in PHP. PHP's functions and methods can have optional parameters for which you do not have to provide an argument, and because PHP is dynamically-typed, a parameter can accept an argument of any type (and within the function body discriminate between types) if it needs to. Also, PHP has fewer and more generalised types than some other languages do, so where a C# method might have overloaded versions taking a UInt8, UInt16, UInt32, SInt8, SInt16 and so on, an equivalent PHP method might only take PHP's single universal integer type.

Of course, this doesn't perfectly cover all the use cases of overloading. You can't, for example, directly express in PHP that a function takes one argument or another, but not both. That said, however, I don't think this is necessarily a big problem: you can write two functions with different names.

Having to write differently-named functions may well be a good thing, too, speaking from having had to deal with APIs in languages where a "single" method can have more than a dozen overloaded forms, each behaving differently, yet they all look confusingly similar at the call-site.

Anyway, thank you for asking!

--
Andrea Faulds
https://ajf.me/

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to