Skip to content
This repository was archived by the owner on Jan 6, 2020. It is now read-only.
This repository was archived by the owner on Jan 6, 2020. It is now read-only.

[rfc] Immutable variant #4

@stefanotorresi

Description

@stefanotorresi

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 BigNumber implement it.
  • Introduce a new class BigNumberImmutable, also implementing BigNumberInterface.

This could target a minor 1.x release, I guess 1.2.

2. Non BC Safe

  • Rename BigNumber to BigNumberMutable.
  • Extract a new interface BigNumber from it, and make it implement it.
  • Introduce a new class BigNumberImmutable, also implementing BigNumber.

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions