Re: Method overload support

From: Date: Sun, 25 Apr 2021 21:09:00 +0000
Subject: Re: Method overload support
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-114165@lists.php.net to get a copy of this message
On Sun, 25 Apr 2021 at 16:12, David Rodrigues <david.proweb@gmail.com> wrote: > I know that this discussion comes back from time to time, but now that PHP > is quite strong in relation to argument typing, I think it is worthwhile to > have a quick discussion about it. Maybe to PHP 9. > > One of the things I remember that was a big problem was the performance > impact in determining what the ideal method would be, without testing it > each time. > > 1. The test only needs to happen for overloaded methods. So, most of the > time, no change will occur. > > 2. Opcache may be able to help indicate which method will be executed, when > it is available. And perhaps, in some situations, it is possible to > determine which method will be tested as a priority, without having to test > all the time (in a loop, for example). > > function printId(User $user) { return a($user->id); } > function printId(int $userId) { printf($userId); } > > foreach ([1, 2, 3, 4, $userInstance] as $userId) { > printId($userId); > // First time: > // 1. Overload detected, but the best option is unknown; > // 2. Test option #1: rejected; > // 3. Test option #2: accepted and used; > // Next times: > // 1. Overload detected, the last occurrence was option #2, accepted > and used; > // 2. Option #2 can't be used for $userInstance, retest all options > (except #2). > } > > 3. Poorly typed functions or methods cannot be overloaded. > > function printId(User $user) { ... } > function printId($user) { ... } // Fatal error > > > Atenciosamente, > David Rodrigues > This was brought up four years ago (https://externals.io/message/93831) and you brought this up two years ago, but I don't think anything has changed to change the answer. The responses in the four-years-ago post contain some good explanations. Unless you have a draft implementation that fixes the issues mentioned I'm not sure it's productive to bring it up again. Best wishes, Matt

« previous php.internals (#114165) next »