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

 ID:                 60302
 Comment by:         s...@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] der...@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

Reply via email to