Re: [PHP-DEV] [RFC] Duration class

[email protected] (Tim Düsterhus)
Newsgroups php.internals
Message-ID <[email protected]>
Hi

Am 2026-06-29 22:38, schrieb ignace nyamagana butera:
> How about this
> 
> Duration::fromHours(5)->dividedBy(5); //returns  
> Duration::fromHours(1);
> and
> Duration::fromHours(50->dividedInto(Duration::fromHours(1)); // returns
> ['factor' => 5, 'remainder' => Duration::fromSeconds(0)];
> 
> Duration::dividedInto in my example returns an array but I would have 
> no
> issue if it would return a DTO instead.
> This way the naming seems easy to remember IMHO
> 
> - "by" -> integer,
> - "into" -> duration
> 
> and it reads naturally I think.

That is a great suggestion, thank you. I agree this reads pretty 
naturally, because the “divide into” requires the presence of a “unit” 
to feel complete, whereas the “divide by” doesn't. Derick also confirmed 
that the suggestion sounds reasonable to him.

To remain consistent with `bcdivmod()`, the function should return a 
2-tuple (i.e. `array{0: int, 1: Duration}`). This makes destructuring 
very convenient:

     [$fullDurations, $remainder] = $oneHour->divideInto($sevenMinutes);

see https://news-web.php.net/php.internals/124098 for the previous 
discussion.

I don't plan to add `divideInto()` to the initial set of methods in PHP 
8.6, but would be open to it if anyone feels having it right away is 
useful or necessary. Please speak up in that case. And please also do if 
you disagree with the divideBy() + divideInto() pair.

Best regards
Tim Düsterhus
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.