Req #60302 [Com]: DateTime::createFromFormat should new static(), not new self()
| From: | seld@php.net | Date: | Mon, 14 May 2012 10:09:57 +0000 |
| Subject: | Req #60302 [Com]: DateTime::createFromFormat should new static(), not new self() | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-170420@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=60302&edit=1
ID: 60302
Comment by: seld@php.net
Reported by: kontakt at beberlei dot de
Summary: DateTime::createFromFormat should new static(), not
new self()
Status: Assigned
Type: Feature/Change Request
Package: Date/time related
PHP Version: Irrelevant
Assigned To: derick
Block user comment: N
Private report: N
New Comment:
I added this in my subclass. Works well as a workaround, but it would be nice to have it in code
indeed.
public static function createFromFormat($a, $b, $c = null)
{
$date = $c ? parent::createFromFormat($a, $b, $c) : parent::createFromFormat($a, $b);
return new static('@'.$date->format('U'), $date->getTimeZone());
}
Previous Comments:
------------------------------------------------------------------------
[2011-11-14 21:51:54] derick@php.net
Had a quick look at this, but it wasn't as simple as I thought. Needs some more investigation.
------------------------------------------------------------------------
[2011-11-14 20:00:54] kontakt at beberlei dot de
Description:
------------
I want to extend DateTime, but without DateTime::createFromFormat, actually creating instances from
my class, i.e. "MyDateTime::createFromFormat" this is rather problematic.
Test script:
---------------
<?php
class MyDateTime extends DateTime { }
$d = MyDateTime::createFromFormat('Y-m-d', '2011-01-01');
echo get_class($d);
Expected result:
----------------
MyDateTime
Actual result:
--------------
DateTime
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=60302&edit=1