Edit report at https://bugs.php.net/bug.php?id=79975&edit=1
ID: 79975
Comment by: corey dot taylor dot fl at gmail dot com
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:
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.
Previous Comments:
------------------------------------------------------------------------
[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.
------------------------------------------------------------------------
[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.
------------------------------------------------------------------------
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