# DateTimeImmutable::modify() return type: doc or language bug?

- Thread: https://externals.io/message/119223
- Original archive: https://news-web.php.net/php.internals/119223
- Started by Philip Hofstetter on 2023-01-03T09:48:56+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #119223 — Philip Hofstetter — 2023-01-03T09:48:56+00:00

URL: https://externals.io/message/119223#119223

Hi,

`DateTimeImmutable::modify()` is documented as returning
`DateTimeImmutable`, but it seems to actually be more specifically
returning `static`:

https://3v4l.org/j9ZSo

Now I'm wondering whether this is a documentation issue (where it
should return `static|false` and has not been updated to account for
that return type's availability yet) or whether this is a language
issue and the method is supposed to be returning `DateTimeImmutable`
instead, though that's probably a considerable BC break.

If you agree that it's a doc bug, I'll submit a PR.

Regards,

Philip

---

## #119224 — Larry Garfield — 2023-01-03T15:06:45+00:00

URL: https://externals.io/message/119223#119224 · In reply to: #119223

> Hi,
>
> `DateTimeImmutable::modify()` is documented as returning
> `DateTimeImmutable`, but it seems to actually be more specifically
> returning `static`:
>
> https://3v4l.org/j9ZSo
>
> Now I'm wondering whether this is a documentation issue (where it
> should return `static|false` and has not been updated to account for
> that return type's availability yet) or whether this is a language
> issue and the method is supposed to be returning `DateTimeImmutable`
> instead, though that's probably a considerable BC break.
>
> If you agree that it's a doc bug, I'll submit a PR.
>
> Regards,
>
> Philip

That sounds like a doc bug to me.  It's likely the doc page hasn't been updated since before the `static` return was added.  The code looks like it's doing the right thing.

Doc PR, please and thank you!

--Larry Garfield

---

## #119225 — Christoph M. Becker — 2023-01-03T15:43:49+00:00

URL: https://externals.io/message/119223#119225 · In reply to: #119223

> `DateTimeImmutable::modify()` is documented as returning
> `DateTimeImmutable`, but it seems to actually be more specifically
> returning `static`:
>
> https://3v4l.org/j9ZSo
>
> Now I'm wondering whether this is a documentation issue (where it
> should return `static|false` and has not been updated to account for
> that return type's availability yet) or whether this is a language
> issue and the method is supposed to be returning `DateTimeImmutable`
> instead, though that's probably a considerable BC break.

It is not recommendable to extend DateTime(Immutable); these classes
have not been designed for that; for instance (and that may actually be
the only issue), __construct() is not called (<https://3v4l.org/4Y7jU>).
 Of course, we cannot easily make them final, but I don't think we
should encourage users to subclass by documenting return types as static
– besides that the current return type is not wrong (it's just not as
specific as it could be).

> If you agree that it's a doc bug, I'll submit a PR.

As of PHP 8.0.0, the single-source-of-truth regarding the signatures are
the stub files[1].  The signatures in the PHP manual can be
automatically generated from these.  Therefore the stubs would need to
be changed (first).

[1] <https://github.com/php/php-src/blob/master/ext/date/php_date.stub.php>

--
Christoph M. Becker

---

## #119226 — drealecs@gmail.com — 2023-01-03T17:08:01+00:00

URL: https://externals.io/message/119223#119226 · In reply to: #119225

> 
>
> > […]
>
> It is not recommendable to extend DateTime(Immutable); these classes
> have not been designed for that; for instance (and that may actually be
> the only issue), __construct() is not called (<https://3v4l.org/4Y7jU>).
>

But interestingly __clone() is called: https://3v4l.org/A7Gd2

I agree that extending should not be the way to go. However, there are some
well known libraries that do it, like Carbon.


 Of course, we cannot easily make them final, but I don't think we
> should encourage users to subclass by documenting return types as static
> – besides that the current return type is not wrong (it's just not as
> specific as it could be).
>
> > […]
>
> As of PHP 8.0.0, the single-source-of-truth regarding the signatures are
> the stub files[1].  The signatures in the PHP manual can be
> automatically generated from these.  Therefore the stubs would need to
> be changed (first).
>
> [1] <https://github.com/php/php-src/blob/master/ext/date/php_date.stub.php
> > […]
>
> --
> Christoph M. Becker
>
> --
> 
> To unsubscribe, visit: https://www.php.net/unsub.php
