Re: [RFC] [Discussion] Support object type in BCMath

From: Date: Fri, 05 Apr 2024 10:57:53 +0000
Subject: Re: [RFC] [Discussion] Support object type in BCMath
References: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16  Groups: php.internals 
Request: Send a blank email to internals+get-122963@lists.php.net to get a copy of this message
On 05/04/2024 10:48, Tim Düsterhus wrote:
Your Money example would allow for unsound and/or non-sense behavior, such as:
    $fiveEuros = new Money(5, 'EUR');
    $tenDollars = new Money(10, 'EUR');
    $what = $fiveEuros + $tenDollars;
What would you expect to be in $what? A BcMath\Number(15)?
Yep, since the add method is final and will return a Number then Number(15) is the only possible result. And even if you did `(new Money(5, 'EUR')) + (new Money(15, 'EUR'))`, the result would be the Number 15, not a money. Equally if it was extended to add the isPrime method as soon as you do any calculation on it you'd get a number without the isPrime method, so it would be of very limited use. That could be avoided by returning static instead of self, but that's makes addition non-commutative which would be confusing, and using `new static` sort of requires the constructor to be final, which would make the Money case impossible. So yes I'd say you've convinced me that the Number class does need to be a Final class.

« previous php.internals (#122963) next »