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

Reply via email to