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 <internals@lists.php.net>; 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 <internals@lists.php.net>; 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: <imsop.php@rwec.co.uk>
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 <internals@lists.php.net>; 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 <internals@lists.php.net>; 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: <xms:uyURZvPvmIhMHd2e3g-tpVxcdB4wX5_64rejsmViDZ5yZfSyewbgAQ>
    <xme:uyURZp-YrMZVe98Rfl2g6TQFaKDkTbGdo8dS-JqShnPTo3s4Q4Az_KcArnhkNjvCj
    OpZN_UVSTr3s40bdAk>
X-ME-Received: <xmr:uyURZuQtkDRzkYq0_jGzJHuy4Pe8PuMnMGcdFw6pODSghWDisj0wWDVl53quwCE_tTtwUadfKBRpceOt8y1CCkwt_rHbeQVSQ-huzBhtewnRFQPfvzHW>
X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudegvddgfeduucetufdoteggodetrfdotf
    fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen
    uceurghilhhouhhtmecufedttdenucenucfjughrpeffhffvufgfjghfkfggtgfgsehtqh
    hmtddtreejnecuhfhrohhmpedftfhofigrnhcuvfhomhhmihhnshculgfkoffuohfrngdf
    uceoihhmshhophdrphhhphesrhifvggtrdgtohdruhhkqeenucggtffrrghtthgvrhhnpe
    ehleffteeigfevudetfedugedtudevledugeeugeelheeihfehgfdtkeevvefgleenucev
    lhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehimhhsohhprd
    hphhhpsehrfigvtgdrtghordhukh
X-ME-Proxy: <xmx:uyURZjvOsTX4FNiFG2qvyimK4ikpMhUN5Wd6zM8tQTLxhey6HjV0cA>
    <xmx:uyURZneVpSBktYzLlwKyM6K9Zpaa1YATAa14-kHhQneHtPlz2mo18g>
    <xmx:uyURZv0pZCcPGvBfhJuPA6d6LygU_5t-Oba9VDh5C9Wpb8Qayr5NNg>
    <xmx:uyURZj_AOFHPl2Eq8lJwsvrbE1u4XEP1QQGAYy4LwzHo3bySVocxCQ>
    <xmx:uyURZoo6K03SQJG-fKwYXGFgTYTMJ4jni3gzpjSclU03IqFZi0k8mv8ijsp2>
Feedback-ID: id5114917:Fastmail
Received: by mail.messagingengine.com (Postfix) with ESMTPA for
 <internals@lists.php.net>; 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: <CAMrTa2Geo1XHFxfa1Mj6tXcYC-gVwQeUK+Wk0h11d1fELUyZJQ@mail.gmail.com>
References: <4F094EDA-5058-407D-AF39-06FD934FDE1F@sakiot.com> <68CF373E-6ABF-4471-8992-813B4BA1B508@sakiot.com> <904197f4-afb5-401e-9e17-7a655c5449d0@alec.pl> <CAMrTa2EK6ymBQuxeFj7tMV0M2crqCCCR=mLN-FSfuCgrQHEZ6A@mail.gmail.com> <AB663F3F-A888-45F7-B19A-00530A4EDB0C@sakiot.com> <CAMrTa2HOEUWQcPdZE3FZ9JWCS1DWuRTNhhfqGGZOmr=v1Mctdw@mail.gmail.com> <CANS-=pfVuKKfAiu0LUJTrEPnSRaa_nHwJCgMvSORruAwHMsvQg@mail.gmail.com> <CAMrTa2HXD+62TKbaaA-v-mct5zdR5ortUAbtG1R1D22amAgpNA@mail.gmail.com> <CAMrTa2GaK3AUp0WodhNO8RdvoLbbiCk14Gs2oWF6vEs_+qMYNg@mail.gmail.com> <655FEA80-9AB4-4AAD-A310-70ED968C97A2@sakiot.com> <CAMrTa2Fh_-M_Kezi7s_4P4+a+dTyKq=RExd6ARPRDqZYS175kA@mail.gmail.com> <CAMrTa2FrkvNPK6kETNJFNNDn0mL5dxkV=PmdxsysruhhjY=Uig@mail.gmail.com> <8FB87901-02D7-4934-9119-55B21CEDDA9D@sakiot.com> <CAMrTa2EJf2NK2D3J=q7xkAVuNKsjEBDVv+RDmxp_g9DSsjRgwg@mail.gmail.com> <8035a2b2-f8dc-4a25-98eb-1d19c986bc75@bastelstu.be> <CAMrTa2Geo1XHFxfa1Mj6tXcYC-gVwQeUK+Wk0h11d1fELUyZJQ@mail.gmail.com>
Message-ID: <DD7E3EE0-49B2-4FBB-A99D-8A558CC9F185@rwec.co.uk>
Precedence: bulk
list-help: <mailto:internals+help@lists.php.net
list-unsubscribe: <mailto:internals+unsubscribe@lists.php.net>
list-post: <mailto:internals@lists.php.net>
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 <jordan=2Eledoux@gmail=2Ecom> =
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]