# Methods Overloading in PHP

- Thread: https://externals.io/message/113325
- Original archive: https://news-web.php.net/php.internals/113325
- Started by Eugene Sidelnyk on 2021-02-28T12:49:52+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #113325 — Eugene Sidelnyk — 2021-02-28T12:49:52+00:00

URL: https://externals.io/message/113325#113325

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:

---

## #113326 — AllenJB — 2021-02-28T13:07:05+00:00

URL: https://externals.io/message/113325#113326 · In reply to: #113325

>   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.

---

## #113327 — Eugene Sidelnyk — 2021-02-28T13:20:57+00:00

URL: https://externals.io/message/113325#113327 · In reply to: #113326

> 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)



>
> 
> > […]
>
>
> 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.

---

## #113328 — kontakt@beberlei.de — 2021-02-28T13:21:16+00:00

URL: https://externals.io/message/113325#113328 · In reply to: #113325

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
> 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:

---

## #113330 — Rowan Tommins — 2021-02-28T17:20:37+00:00

URL: https://externals.io/message/113325#113330 · In reply to: #113327

On 28 February 2021 13:20:57 GMT, Eugene Sidelnyk <zsidelnik@gmail.com> 
wrote:
> > […]
>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]
