On Tue, Aug 10, 2021 at 8:46 PM Mike Schinkel <m...@newclarity.net> wrote:

>
> Either way if PHP can identify a value object then it could limit operator
> overloads to just classes that are value objects by whatever approach PHP
> chooses to distinguish.
>
>
> -Mike
> [*] Ironically I mentioned you because I thought you would be appreciate
> seeing another reason PHP could benefit from value objects. But as they
> say, no good deeds go unpunished.
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: https://www.php.net/unsub.php
>
>
Here is a quick and dirty example of just the complex number use case. It
assumes that the operator overloads already are built in. A couple of
things of note:

1. The actual mathematical logic to cover all the possible input and return
types is extensive. Though the execution time and complexity are quite
limited, the code complexity is high due to the number of conditionals.
2. I chose to implement the various operator overload functions in
different ways to illustrate different methods that might accomplish the
same task in userland code. For instance, in some cases I typed against the
SimpleNumber abstract, while in others I expanded SimpleNumber out into its
concrete implementations, Real and Imaginary.
3. I only implemented the _add() and __mul() methods for this example, as
the reality is that getting into some of the more complex operations would
be so much code that it wouldn't be a digestible example. The __pow()
method for ComplexNumber would be in excess of 100 lines and also require a
polar coordinate representation within the class, for instance.

This isn't a working implementation, in that this code will produce errors
if run in PHP (due to the absence of operator overloads). Please note that
if we were to go the route of creating domain-specific classes to cover
this use case, the actual classes would be many, many times longer than
this. This is some of the simplest logic involved in this particular use
case, which is why I chose it as the example, and that perhaps illustrates
why I am willing to explore such objects but do not believe they are the
best choice at this time.

https://github.com/JordanRL/operator-overloading-rfc/tree/master/use-cases/complex-numbers

Jordan

Reply via email to