Bug #79975 [Opn]: DateTime::createFromFormat returns static type

From: Date: Fri, 21 Aug 2020 09:47:56 +0000
Subject: Bug #79975 [Opn]: DateTime::createFromFormat returns static type
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-228699@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79975&edit=1

 ID:                 79975
 Updated by:         derick@php.net
 Reported by:        corey dot taylor dot fl at gmail dot com
 Summary:            DateTime::createFromFormat returns static type
 Status:             Open
 Type:               Bug
 Package:            *General Issues
 PHP Version:        8.0.0beta1
 Block user comment: N
 Private report:     N

 New Comment:

I don't there should be a reason why a factory creation method should call a constructor?
Normal PHP also does not do this: https://3v4l.org/ekOtT


Previous Comments:
------------------------------------------------------------------------
[2020-08-21 09:38:29] corey dot taylor dot fl at gmail dot com

Looking into this further, I see how calling the constructor would be tricky.

The time would need to be calculated from the createFromFormat parameters and then converted to a
standard/ISO format to pass to the constructor to actually instantiate the object.

However, it's really an awkward scenario to deal with since there's no initialization of
properties through the derived constructor.

------------------------------------------------------------------------
[2020-08-15 16:23:35] mark at mark-story dot com

If the subclass is going to be returned by DateTime::createFromFormat() shouldn't its
constructor also be used?

https://3v4l.org/LqeX2

Shows that while the subclass is returned, its constructor hasn't been returned which is
something I would expect.

------------------------------------------------------------------------
[2020-08-14 09:33:57] corey dot taylor dot fl at gmail dot com

One issue we will have is knowing when this object was created.

https://3v4l.org/k5gDZ

Is there no indication that the derived class was instantiated from createFromFormat()?

------------------------------------------------------------------------
[2020-08-13 23:17:20] corey dot taylor dot fl at gmail dot com

As part of the Chronos (date/time library), we have a Date type that wraps DateTime and handles
ensuring date-only values.

There is logic that checks if creating an instance from a Date object or not to determine what to
fix up. With this change, the unfixed instance is simply cloned.

Since DateTime::createFromFormat('Y-m-d') takes the current time all this logic was added
to ensure construction was valid.

To be clear, this behavior seems like the correct behavior and should have been the original
behavior, but since this will take some re-design to work with PHP 7 and 8, it's worth checking
this won't revert back.

------------------------------------------------------------------------
[2020-08-13 22:38:16] derick@php.net

This is indeed an intended big fix. As this change doesn't violate LSP (the inherited child
class still follows the is_a relationship with DateTime), how does it cause a problem in your
situation?

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=79975


--
Edit this bug report at https://bugs.php.net/bug.php?id=79975&edit=1


Thread (11 messages)

« previous php.bugs (#228699) next »