Re: Overriding GMP objects

From: Date: Thu, 27 Jun 2024 22:11:06 +0000
Subject: Re: Overriding GMP objects
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message


On Thu, Jun 27, 2024, at 23:46, Jordan LeDoux wrote:
> 
> 
> On Thu, Jun 27, 2024 at 2:35 PM Rob Landers <[email protected]> wrote:
>> __
>> 
>> 
>> On Thu, Jun 27, 2024, at 06:07, Saki Takamachi wrote:
>>> 
>>> I agree with Gina. Being able to change the class of a computed result of an inherited
>>> child class causes several problems.
>>> 
>>> The points raised in the BCMath\Number discussion can be summarized as
>>> follows.
>>> 
>>> - If it is possible to perform calculations on parent Class and child Class, what
>>> should the resulting class be?
>> 
>> Ask the class what it wants to be?
>> 
>>> - Multiplying the distance class and the speed class should give you the time class.
>> 
>> That would depend on the library’s implementation. 
>> 
>>> - Similarly, multiplying two distance classes together should yield an area class.
>> 
>> See above. 
>> 
>>> - If only child classes have a property that should be specified in the constructor,
>>> how do you determine the value of this property in the result class? Perhaps it is possible to
>>> determine the value of the property, but in that case the commutativity of the computation should be
>>> lost.
>> 
>> This can be handled via the child class in static methods, per the library specifications.
> 
> How is the library supposed to do that in the absence of operator handler functions that can be
> overridden, or at least called when an operator is encountered?
> 
> Jordan

That’s what this email is about: adding those functions to the base GMP class as static protected
methods.

A time class would look something like:

static protected add(GMP $left, GMP $right) {
  if($left instanceof self && $right instanceof self) return new self(parent::add($left,
$right));
  throw new LogicException('can only add time to time');
}

— Rob


Thread (11 messages)

« previous php.internals (#123976) next »