-
-
Notifications
You must be signed in to change notification settings - Fork 7
[rfc] Immutable variant #4
Description
I've been struggling with mutability when using this library, IMHO it has the same issues as \DateTime, and it would greatly benefit from an immutable variant.
Basically, I would like to do the following:
$foo = new BigNumberImmutable(2);
$bar = $foo->multiply(2);
assert($foo == '2');
assert($bar == '4');
assert($bar !== $foo); I have seen that there were moontoast#1 and moontoast#2 opened in the old repo, but it has been pretty much abandoned so I'd like to pick this up.
With that proposal, I didn't particularly like the introduction of a constructor parameter in the parent and a child class, so I would follow a different approach.
While I get started, I'd like your feedback, because I'm considering two options:
1. BC Safe
Follow exactly the same approach that was implemented for \DateTime:
- Introduce a new
BigNumberInterface. - Make
BigNumberimplement it. - Introduce a new class
BigNumberImmutable, also implementingBigNumberInterface.
This could target a minor 1.x release, I guess 1.2.
2. Non BC Safe
- Rename
BigNumbertoBigNumberMutable. - Extract a new interface
BigNumberfrom it, and make it implement it. - Introduce a new class
BigNumberImmutable, also implementingBigNumber.
Over approach 1, this would have the arguable benefit of better expressing the type hierarchy, but I realise that this is more a stylistic choice than anything else, and I don't want to start any religion war on the *Interface suffix.
The downside of this approach is that it would of course have to target a major release, I guess 2.0, because BigNumber would be no longer "newable".
That said, migrating to the new version would only require a use alias statement.