Edit report at https://bugs.php.net/bug.php?id=79975&edit=1
ID: 79975
Updated by: nikic@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:
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;
}
}
Previous Comments:
------------------------------------------------------------------------
[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.
------------------------------------------------------------------------
[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()?
------------------------------------------------------------------------
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