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.
-
The test only needs to happen for overloaded methods. So, most of the
time, no change will occur. -
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).
}
- Poorly typed functions or methods cannot be overloaded.
function printId(User $user) { ... }
function printId($user) { ... } // Fatal error
Atenciosamente,
David Rodrigues
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.
The test only needs to happen for overloaded methods. So, most of the
time, no change will occur.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).
}
- Poorly typed functions or methods cannot be overloaded.
function printId(User $user) { ... }
function printId($user) { ... } // Fatal errorAtenciosamente,
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
- Poorly typed functions or methods cannot be overloaded.
function printId(User $user) { ... }
function printId($user) { ... } // Fatal error
Since the second is the same as mixed $user
, why couldn’t the engine
pick the most specific method for the signature? If a User
instance
is passed, then pick the first method. Otherwise, pick the second.
Cheers,
Ben