Re: [RFC] Duration class
| From: | Larry Garfield | Date: | Mon, 22 Jun 2026 18:52:56 +0000 |
| Subject: | Re: [RFC] Duration class | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-131477@lists.php.net to get a copy of this message | ||
On Sat, Jun 20, 2026, at 6:45 AM, ignace nyamagana butera wrote:
> On Sat, Jun 20, 2026 at 1:01 PM Tim Düsterhus <tim@bastelstu.be> wrote:
>> Hi
>>
>> Am 2026-06-19 12:25, schrieb ignace nyamagana butera:
>> > However, I would advise against introducing Duration::add and
>> > Duration::sub
>> > methods. Instead, I would recommend providing a single method,
>> > Duration::sum(Duration
>> > ...$durations): self.
>> >
>> > Given that a Duration already carries a sign and may therefore be
>> > either
>> > positive or negative, the presence of separate add and sub methods
>> > could
>> > create an implicit and potentially misleading notion of directional
>> > behavior.
>> >
>> > In contrast, a sum method using variadic arguments would allow multiple
>> > Duration instances to be combined in a natural and consistent manner.
>> > It
>> > would also avoid implying any expectation regarding the resulting sign,
>> > which may legitimately be either positive or negative depending on the
>> > input values.
>>
>> Can you clarify if you expect the
sum() method to be a
>> static method
>> or an instance method? If it's a static method, what would you expect
>> Duration::sum() (with an empty list of durations) to result
>> in? Should
>> it be an Error, Duration::fromSeconds(0), or perhaps
>> something
>> entirely different?
>>
>> Best regards
>> Tim Düsterhus
>>
>> PS: Please don't forget to reply below the quoted parts; and to cut the
>> quoted parts to the relevant portion.
>
> Hi Tim,
>
> I would make the method static and if no argument is given I would
> expect an ArgumentCountError to be thrown just like with array_sum.
>
> Best regards,
> Ignace
(Repeating here per Tim's request.)
I am 100% against a static method for adding durations. That's entirely pointless. Even if
we're just using methods and not operators, $dur3 = $dur1->add($dur2) is the way to go.
--Larry Garfield