Re: [RFC] Duration class

From: Date: Sat, 20 Jun 2026 11:47:34 +0000
Subject: Re: [RFC] Duration class
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-131421@lists.php.net to get a copy of this message
Hi Am 2026-06-19 18:35, schrieb Larry Garfield:
What would those be? Personally I'd want to see operators for add and sub, at least, but I'm not sure what else would be useful.
I find the current behavior of operator overloading very unintuitive, because there is no obvious way of defining an expected signature, it's not in the stub, and if the overloaded operator throws, the stack trace will not provide any indication to what has happened either, see:
    php > var_dump(new GMP(5) + new stdClass());
    PHP Warning:  Uncaught TypeError: Number must be of type GMP|string|int, stdClass given in php shell code:1
    Stack trace:
    #0 {main}
      thrown in php shell code on line 1
I believe the overloaded comparison handler is a reasonable middle-ground and I wouldn't necessarily be against adding the other overloads when the entire user-story is better. But Duration::fromSeconds(5) + 6 throwing with a TypeError (Duration + int is not meaningful) without being able to look up which operations are supported is not helping anyone.
7. Naming consistency ... add/sub vs. multiplyBy. Why not addBy/sub[tract]By or multiply to be more consistent?
I concur here.
See my reply to Marc.
9. negate() I would expect to get back a negative Duration. Also, it sounds like modifying the duration but I'm assuming a new instance gets returned. What about inverted()?
Good point. negated() would be fine, I think. (As noted elsewhere, sort() vs sorted() is a very common pattern for "modify in place" vs. "make new", across a number of languages.)
See my reply to Marc. Best regards Tim Düsterhus

« previous php.internals (#131421) next »