Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122993 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id B6E531A009C for ; Sat, 6 Apr 2024 10:36:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1712399836; bh=oczVz5hR/fuAg7ieSOzZ2ejFTyHSNn+//PMsEcsCmXk=; h=Date:From:To:Subject:In-Reply-To:References:From; b=jNt7IVDP8d17lcMrg+phINq4zyVx7upieVI+3eR6ATWTmhmfpxssaXbZTGM7AZIZz Kyedm+d6cQdQYU/GuUHXiA+7yykQIfAPwARpHkdMByowqeHnjM6fa3Oz52Fq38/JiB Ni+r0tKRJZ3jkLHA4fuOGdS0uYwvJwKadVV0PvBNsGrI9hQwItlg4I2NW88fu8cTUy QXcLvAH26khpv0GgILMBqxOyByk4D/K6vbPEjTXX7DAcyasGTsWUQ03il7SQWCwd3Y E0Da/rGBfPeTEzHxHuOrXr9Dwj5TlGUlCBvOYXtys04BCLaFrSkiM5UwwauUTXA4JQ 0YUyINqbD4QTQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 49A9B18006D for ; Sat, 6 Apr 2024 10:37:15 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from out4-smtp.messagingengine.com (out4-smtp.messagingengine.com [66.111.4.28]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 6 Apr 2024 10:37:14 +0000 (UTC) Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailout.nyi.internal (Postfix) with ESMTP id 9DB0D5C008C for ; Sat, 6 Apr 2024 06:36:43 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Sat, 06 Apr 2024 06:36:43 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rwec.co.uk; h=cc :content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm2; t=1712399803; x=1712486203; bh=oczVz5hR/fuAg7ieSOzZ2ejFTyHSNn+//PMsEcsCmXk=; b= fuLLEArb/TI/NYcKK1G1mHqxYpKRrlE3TaiM5HkCjNtGADdcYvQnYIZ+Ntoba2I6 TZxS4NDBz1Yg7R+ZDufLi+NxHsavdbKKdRyQCUgQSI+s2WWR5u7tZnNwcYHhDifN ECvNnaB0B0YFGsZqA/8tls15OAMC8NfMq8TtpLa3ZAae2PRBGR8IWUj5qc9NVlIg gQlrfVl2HsGPn9sb5f81HiDOzwePWusWwrdrdtZ4ymghX5QkmMyI0fWoB1jBPjnf iXSG8RZIE5A02khf+JSCr83KpNiMMcTXAQ8Ec5M95oee3cGTAQRK5hWLAzZf69xB IvHzilF3Oa2/THtRpi3pnQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1712399803; x= 1712486203; bh=oczVz5hR/fuAg7ieSOzZ2ejFTyHSNn+//PMsEcsCmXk=; b=M SlY8Oi9veiR9EMgr0r5pB/Qfx9cSkmE0/qPVZPBibtFvLTKGimdyyK6i+uj1zZgK 1W+Lom7ghJKoUHu3RsiedFKZRp1gQAlhwqG8SAw4eWb0d9B39gwfb/aOMwFrm2+l lXR3hWnpp4wAa5nTZun6uJ8h52AofOWPeyUnwLCJbBbYZnx7ywToPA/bfrvMqd3m MrQyWigKElXTA9jxaAO1L+EqLS68eyLi9V4UIkdDT6LwsXnqMPl4KGqJLDk08lA7 VhQ0VO4qOsHK+3sE6prZ/PJm4HudrZSi+2TXfxsIbPjXG4MzAngb+JpVprfUQg02 0JgeAVkVdYku1arOq5OBA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudegvddgfeduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpeffhffvufgfjghfkfggtgfgsehtqh hmtddtreejnecuhfhrohhmpedftfhofigrnhcuvfhomhhmihhnshculgfkoffuohfrngdf uceoihhmshhophdrphhhphesrhifvggtrdgtohdruhhkqeenucggtffrrghtthgvrhhnpe ehleffteeigfevudetfedugedtudevledugeeugeelheeihfehgfdtkeevvefgleenucev lhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehimhhsohhprd hphhhpsehrfigvtgdrtghordhukh X-ME-Proxy: Feedback-ID: id5114917:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Sat, 6 Apr 2024 06:36:42 -0400 (EDT) Date: Sat, 06 Apr 2024 11:36:40 +0100 To: internals@lists.php.net Subject: Re: [PHP-DEV] [RFC] [Discussion] Support object type in BCMath User-Agent: K-9 Mail for Android In-Reply-To: References: <4F094EDA-5058-407D-AF39-06FD934FDE1F@sakiot.com> <68CF373E-6ABF-4471-8992-813B4BA1B508@sakiot.com> <904197f4-afb5-401e-9e17-7a655c5449d0@alec.pl> <655FEA80-9AB4-4AAD-A310-70ED968C97A2@sakiot.com> <8FB87901-02D7-4934-9119-55B21CEDDA9D@sakiot.com> <8035a2b2-f8dc-4a25-98eb-1d19c986bc75@bastelstu.be> Message-ID: Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: imsop.php@rwec.co.uk ("Rowan Tommins [IMSoP]") On 5 April 2024 19:30:24 BST, Jordan LeDoux = wrote: >A composed class >does not somehow prevent the accidental error of mixing currencies, it ju= st >moves where that error would occur It does not prevent someone accidentally *attempting* to mix currencies, b= ut it absolutely prevents that mistake leading to bogus values in the appli= cation, because the methods available can all detect it and throw an Error= =2E=20 If the class has a mixture of currency-aware methods, and methods / operat= or overloads inherited from Number, you can end up getting nonsensical resu= lts instead of an Error=2E >If you want an actual answer about how a Money class would actually work = in >practice, it would likely be something like this: > >``` >// $val1 and $val2 are instances of the Money class with unknown currenci= es >$val1Currency =3D $val1->getCurrency(); >$val2Currency =3D $val2->getCurrency(); >$val1 =3D $val1->convertToCommonCurrency(); >$val2 =3D $val2->convertToCommonCurrency(); >// perform the necessary calculations using operators >$answer =3D $answer->convertToCurrency($userCurrency); >``` You have missed out the key section: how do you actually add the two numbe= rs? The addition MUST enforce the precondition that its operands are in the= same currency; any other behaviour is nonsensical=2E So the definition of = that method must be on the Money class, not inherited from Number=2E=20 If the add() method on Number is final, you'll need to define a new method= $val1->addCurrency($val2)=2E The existing add() method and operator overlo= ad will be inherited unchanged, but calling them won't just be useless, it = will be dangerous, because they can give nonsensical results=2E=20 That's what makes composition the better design in this case, because the = Money class can expose only the methods that actually have useful and safe = behaviour=2E The fact that the composed class can't add its own operator overloads is u= nfortunate; but allowing inheritance wouldn't solve that, because the inher= ited overloads are all wrong anyway=2E Regards, Rowan Tommins [IMSoP]