Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:108812 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 13038 invoked from network); 2 Mar 2020 19:04:21 -0000 Received: from unknown (HELO localhost.localdomain) (76.75.200.58) by pb1.pair.com with SMTP; 2 Mar 2020 19:04:21 -0000 To: internals@lists.php.net Date: Mon, 2 Mar 2020 17:23:19 +0000 (UTC) Message-ID: <479ea7da1e5738d7bed6c8dd773c@news.php.net> References: <005101d5edae$7b7c3e10$7274ba30$@gmx.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=iso-8859-1; format=flowed X-Newsreader: JetBrains Omea Reader 1098.1 X-Posted-By: 81.166.177.73 Subject: Re: [RFC] Userspace operator overloading From: terje.slettebo@sandefjordbredband.net (=?iso-8859-1?Q?Terje=20Sletteb=f8?=) > On 15/02/2020 22:05, jan.h.boehmer@gmx.de wrote: > > How many of you would prefer a interface solution for operator > overloading? > I wonder if the RFC voting should include the option to choose between > either the magic method approach (with the syntax proposed in the > current > RFC version) or using interfaces. > For an interface version I would suggest these interfaces: > ArithmeticOperators (implements +, -, *, /), PowOperator (**), > ModuloOperator (%), ConcatOperator (.) and BitwiseOperators (~, &, |, > ^, <<, >>> ). +1 for magic methods. There's too many domains where only a few of the operators makes sense and groupings like ArithmeticOperators would not make sense, not at least one of the motivating examples of this proposal: Being able to write a Money class and associated functionality. Adding and subtracting money makes sense, but it makes no sense to multiply or divide them, and this is just *one* domain. Please, let's not go there and artificially limit the possibilities of future developers. For example, for me, it's quite natural to be able to write code like this: $total_discount = $total * $discount; Here, $total_discount and $total is Money, while $discount is a Percentage, so we should be able to define a method that allows you to multiply Money with Percentage, and return a Money object. Adding Money and Percentage would make no sense, but multiplying them does. Regards, Terje