Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:93840 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 82328 invoked from network); 6 Jun 2016 12:10:38 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 6 Jun 2016 12:10:38 -0000 X-Host-Fingerprint: 137.50.159.184 oa-edu-159-184.wireless.abdn.ac.uk Received: from [137.50.159.184] ([137.50.159.184:14846] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 3B/01-10476-B3865575 for ; Mon, 06 Jun 2016 08:10:36 -0400 Message-ID: <3B.01.10476.B3865575@pb1.pair.com> To: internals@lists.php.net References: Date: Mon, 6 Jun 2016 13:10:31 +0100 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:43.0) Gecko/20100101 Firefox/43.0 SeaMonkey/2.40 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Posted-By: 137.50.159.184 Subject: Re: Opinion on function/method name overloading From: ajf@ajf.me (Andrea Faulds) Hi, Others have already raised good technical points (performance, how to distinguish when we have dynamic/weak typing), but I'd like to offer another argument against it, that is, that even if it were possible we might not want overloading anyway. From my perspective, function and method overloading in languages like C++ or C# is useful for mainly two reasons: allowing omitting unnecessary arguments (or, allowing providing additional, more specific arguments), and accepting multiple types for arguments. However, we already have solutions for both of these in PHP. PHP's functions and methods can have optional parameters for which you do not have to provide an argument, and because PHP is dynamically-typed, a parameter can accept an argument of any type (and within the function body discriminate between types) if it needs to. Also, PHP has fewer and more generalised types than some other languages do, so where a C# method might have overloaded versions taking a UInt8, UInt16, UInt32, SInt8, SInt16 and so on, an equivalent PHP method might only take PHP's single universal integer type. Of course, this doesn't perfectly cover all the use cases of overloading. You can't, for example, directly express in PHP that a function takes one argument or another, but not both. That said, however, I don't think this is necessarily a big problem: you can write two functions with different names. Having to write differently-named functions may well be a good thing, too, speaking from having had to deal with APIs in languages where a "single" method can have more than a dozen overloaded forms, each behaving differently, yet they all look confusingly similar at the call-site. Anyway, thank you for asking! -- Andrea Faulds https://ajf.me/