Methods Overloading in PHP

php.internals

[ ]

5 years ago
Hi there! I faced a lack of methods overloading in PHP once again and would like to ask will it be implemented at some point? As well, I want you to show some pros and cons which you see in this feature. Here're some thoughts about this by Yegor Bugayenko:

AllenJB

5 years ago
On 28/02/2021 12:49, Eugene Sidelnyk wrote:
> Hi there! > I faced a lack of methods overloading in PHP once again and would like to > ask will it be implemented at some point? > As well, I want you to show some pros and cons which you see in this > feature. > > Here're some thoughts about this by Yegor Bugayenko:
It looks like your message is missing at least a link. A quick search did however turn up a tweet where Yego suggests it's a code smell (which basically echos my thoughts below): https://twitter.com/yegor256/status/932701585339240449 A quick search shows the most recent discussion on method overloading here appears to be: https://externals.io/message/104073 The big problem with implementing method overloading in PHP is its typing system. It might be possible to implement it in certain cases where types are strictly defined for all parameters and return values, but I suspect there would still be issues over scalar types (particularly int/float) and union types. My personal opinion is that method overloading is unnecessary, and can be detrimental to code readability when not used carefully. Without it I think developers are forced to name methods that do similar things more concisely, while when it's available they're more likely to create a "single" generically named, overloaded method whose behavior can differ depending on the inputs.

[ ]

5 years ago
> It looks like your message is missing at least a link
Sorry, I am using gmail and have attached an image which you probably can't see. ![excerpt from book]( https://habrastorage.org/webt/my/8t/in/my8tin97ukibw6ebh0cbaqhmv64.png) On Sun, Feb 28, 2021 at 3:07 PM AllenJB <php.lists@allenjb.me.uk> wrote:

Rowan Collins

5 years ago
On 28 February 2021 13:20:57 GMT, Eugene Sidelnyk <zsidelnik@gmail.com> wrote:
>> It looks like your message is missing at least a link >Sorry, I am using gmail and have attached an image which you probably >can't >see. > > https://habrastorage.org/webt/my/8t/in/my8tin97ukibw6ebh0cbaqhmv64.png
It's not to do with your mail client or formatting, the mailing list strips all attachments. Screenshots of text are probably the worst way of sharing a quote from something anyway. I don't know what the rest of the article is like, but that quote makes a pretty poor case for overloading. It has very little to do with OOP - SmallTalk, where OO was invented, doesn't have it, but plenty of non-OO languages do. There is some relationship to polymorphism, in that you can consider two classes with the same method name to be overloading that method name based on the type of "$this"; but in that case PHP does have it. If anything, overloading on the types of multiple parameters is something I'd associate more with Functional Programming, where you might have multiple overloads for a global function like add(x, y), without them being "owned by" a particular type. The example given of content(File) vs content(File, Charset) is also not great, because it can be achieved in any language with optional parameters, which does include PHP. In some languages, using overloading would save an if check at run-time, because the compiler would select the correct overload, but PHP's method despatching and type checking are all done at run-time anyway. With union types and named parameters, a lot of cases where overloading would be used in C# or Java can be expressed in a slightly different form in modern PHP. It would sometimes be nice to have multiple constructors, but named constructors are more flexible than overloading by type (e.g. Foo::fromName(string $name) and Foo::fromCode(string $code)), and static methods provide a reasonable stand-in for those. Regards,
-- Rowan Tommins [IMSoP]

Benjamin Eberlei

5 years ago
Von meinem iPhone gesendet
> Am 28.02.2021 um 13:50 schrieb Eugene Sidelnyk <zsidelnik@gmail.com>: > >  Hi there! > I faced a lack of methods overloading in PHP once again and would like to > ask will it be implemented at some point?
The way php implements methods and dynamic calling it is pretty unlikely this will get implemented. See more info here: https://github.com/Danack/RfcCodex/blob/master/method_overloading.md