php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #75195 DateTimeInterface does not include add method
Submitted: 2017-09-12 17:06 UTC Modified: 2022-05-20 14:36 UTC
Votes:5
Avg. Score:4.0 ± 0.9
Reproduced:3 of 3 (100.0%)
Same Version:2 (66.7%)
Same OS:2 (66.7%)
From: richard dot carree dot com at gmail dot com Assigned:
Status: Not a bug Package: Date/time related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2017-09-12 17:06 UTC] richard dot carree dot com at gmail dot com
Description:
------------
Both of DateTime and DateTimeInterface expose `add` method. As "DateTimeInterface is meant so that both DateTime and DateTimeImmutable can be type hinted for", why it does not include `add` method?


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-09-12 17:08 UTC] richard dot carree dot com at gmail dot com
Sorry, I meant:

Both of DateTime and DateTimeImmutable expose `add` method. As "DateTimeInterface is meant so that both DateTime and DateTimeImmutable can be type hinted for", why it does not include `add` method?
 [2017-09-29 09:05 UTC] heiglandreas@php.net
I wouldn't want to have it included in the interface as the implementation between both is different. Datetime returns itself, while DateTimeImmutable returns a new instance of Datetimeimmutable. So technically the return value is not the sameā€¦
 [2017-09-29 14:49 UTC] richard dot carree dot com at gmail dot com
Both return an object that implements DateTimeInterface.

Couldn't we have something like the following?

```
    /**
     * @return \DateTimeInterface
     */
    public function add(DateInterval $interval); 
```
 [2022-05-20 14:36 UTC] derick@php.net
-Status: Open +Status: Not a bug
 [2022-05-20 14:36 UTC] derick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

The current interface correctly does not include "add", as the behaviour is not the same (self, vs a new instance returned).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 09 13:01:26 2024 UTC