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
+Status: Wont fix
Type: Bug
Package: Date/time related
PHP Version: 8.0.0beta1
Block user comment: N
Private report: N
New Comment:
I am inclined not to change this back now again in PHP 8.2, as it's been two years and changing
it back will just end up causing more work.
Previous Comments:
------------------------------------------------------------------------
[2020-08-24 07:54:24] corey dot taylor dot fl at gmail dot com
Our work-around is constructing the object twice. We take the result of parent::createFromFormat()
and format() the full string then new static() to ensure the constructor is called which is
necessary as it clamps values.
I tried implementing the logic I mentioned of using timelib to calculate the time formatting a
similar string to then construct the instance. However, as my first attempt working in php, figuring
out how to execute the derived constructor and such from the date code proved impossible.
------------------------------------------------------------------------
[2020-08-24 07:33:38] nikic@php.net
Yeah, the current behavior here is not great. I think it should either continue returning self, or
call the constructor for extended classes.
A workaround would be to do something like
class MyDateTime {
public static function createFromFormat(...$args) {
$date = parent::createFromFormat(...$args);
// Do the rest of your initialization here
return $date;
}
}
------------------------------------------------------------------------
[2020-08-21 09:51:32] corey dot taylor dot fl at gmail dot com
I'm sorry, I don't follow your example. It isn't a factory. It's just a static
function that echos.
Can you explain the logic behind a factory not constructing?
------------------------------------------------------------------------
[2020-08-21 09:47:56] derick@php.net
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
------------------------------------------------------------------------
[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.
------------------------------------------------------------------------
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