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

[email protected] (Nick Sdot)
Newsgroups php.internals
Message-ID <[email protected]>
Hey Tim,

On 30.06.26 19:04, Tim Düsterhus wrote:
> 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

This is a new OOP API, maybe we could prioritise DX over consistency 
with `bcdivmod()`? Can we please return a value object like 
"DurationDivision"?

```
final readonly class DurationDivision
{
     public int $factor; // or "quotient"
     public Duration $remainder;
}

$divisionResult = $oneHour->divideInto($sevenMinutes);

$divisionResult->factor // unlike in destructuring, fully optional
$divisionResult->remainder->seconds
```

Although you call the destructuring very convenient, I personally think 
it makes a more awkward API. Would love and prefer to see an VO here!

Nits:
- while not incorrect, I find `Time` for the namespace not ideal. As 
others proposed, I'd also rather go with "Temporal".
- in the "Proposal" section example of the RFC text the `readonly` are 
redundantly on the class and properties; in the later example you only 
have it on the properties.

--

Cheers
Nick
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.