Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:114162 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 49306 invoked from network); 25 Apr 2021 20:08:30 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 25 Apr 2021 20:08:30 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 1D51D1804BD for ; Sun, 25 Apr 2021 13:12:22 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.2 required=5.0 tests=BAYES_40,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-yb1-f180.google.com (mail-yb1-f180.google.com [209.85.219.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sun, 25 Apr 2021 13:12:21 -0700 (PDT) Received: by mail-yb1-f180.google.com with SMTP id q192so8536598ybg.4 for ; Sun, 25 Apr 2021 13:12:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=xcrfP5ZK/RN3CHtJQUpJFhll15Juk+N2HA74gKwgyGk=; b=LbGkCtmd4qW+fkbCEdCG3Oyk/KtsRicY8a3FAnqckVOsMcZyNwg1eBy4yv6u/+vKsU k6W/d5oiwlBki5R1jNUps9gdysfNfn+1fhiBMA/H7gXqafK1kgMiXXa1qXczXI8ZaNa8 Ux3wcigDHlC+vtB8U9rU7cnUOLtNPhoXAbgDGOPjAEd6JWIc/4c9xSpjtRSnbdI6gEe2 ercUdyojETeLcu1Ef6x18bJCjJ9Ebh0IHP6nNmBhvmTM5HsZV8Cd+h/HlB4Ju3dkvP3W JYsmOYqPx+xS4StbJpzhzyYn+JXh58e7vLwppjLYVyGqH6sLP6/w1zkiAg36TzlvDMuS N5Wg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=xcrfP5ZK/RN3CHtJQUpJFhll15Juk+N2HA74gKwgyGk=; b=Vnml2C2wy2cLJm0JKseuq8kiZNe0uL9z25olmLB+DlTRccaTlT3ofbvayPK+3byDZJ 3f0YdmVfp2NKfms5FJSMlH3yPGDfe4+WvVRCBq4pEPs1TBG0TJRuKO83mLq1O+p8t8jL fIoXGVkfvzpG0rAoDE+X7hm/fiKry3VZvgC7lBms7MYLQk83MOTVAjL0XQSmtZxv1Ceq nULPXEPPFtEND90HDVTm7Qr2JqM+3pBdR1Q6Tolv5y+ejQwSM1JiL66DcPywUX3eSpta NQaWUabt4zMuDGbVDwUnpk0xSV5Q9pDgK+okSazx/PliJH9dZL1x1PtVRy26tQN0583g pylg== X-Gm-Message-State: AOAM533u3nGyEBKGnUt3akhDCtY6LXWz5yqcQNjn6C6heamoHe+mxHOo PFFbsnraaV5ScRdnoEt83o+7/RxLC93SDN2/RmHzeK63ZVHYbA== X-Google-Smtp-Source: ABdhPJxCAoAWuD54/ShSLHL2mXb0At6GMh7k08AEgpneDcWum2RPez+A5mLXGnECFQ6YYZKuX5ccZl8UnlI2a/t+G/8= X-Received: by 2002:a25:888b:: with SMTP id d11mr21707621ybl.385.1619381538855; Sun, 25 Apr 2021 13:12:18 -0700 (PDT) MIME-Version: 1.0 Date: Sun, 25 Apr 2021 17:12:07 -0300 Message-ID: To: PHP Internals Content-Type: multipart/alternative; boundary="000000000000611f4705c0d1a5ac" Subject: Method overload support From: david.proweb@gmail.com (David Rodrigues) --000000000000611f4705c0d1a5ac Content-Type: text/plain; charset="UTF-8" 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 --000000000000611f4705c0d1a5ac--