I just learned about this fancy new feature of PHP 5.4. JsonSerializable
! This is perfect for my app.
My app uses DateTime objects, and when I json_encode
them, I get the following (by running json_encode([new DateTime])
):
[{"date":"2013-09-11 15:39:22","timezone_type":3,"timezone":"UTC"}]
Depending on what timezone_type
is, the timezone
value may be different. I haven't found a good way to parse this object in JavaScript.
So, I decided to create my own DateTime class, and have it serialize to JSON how I wanted.
class SerialDateTime extends DateTime implements JsonSerializable{
public function jsonSerialize(){
return ['timestamp' => $this->getTimestamp()];
}
}
When I now run json_encode([new SerialDateTime])
, I get this:
[{"timestamp":1378914190}]
That's much easier to parse in JavaScript.
So, I figured this was a fine solution, but I discovered a problem. Static methods! SerialDateTime::createFromFormat
returns a DateTime
object!
If I do: json_encode([SerialDateTime::createFromFormat('m/d/Y', '10/31/2011')])
, I get:
[{"date":"2011-10-31 15:46:07","timezone_type":3,"timezone":"UTC"}]
Why is this happening? Why doesn't SerialDateTime::createFromFormat
return me a SerialDateTime
object?!
How can I fix this, or do I need to override all the static methods from DateTime
in SerialDateTime
? If I do that, how would I even make a new SerialDateTime
from the createFromFormat
method? How can I "cast" a DateTime
object to SerialDateTime
?
I thought of a workaround, but there's got to be a better way:
public static function createFromFormat($f, $t, $tz=NULL){
$dateTime = call_user_func(
array('SerialDateTime', 'parent::createFromFormat'),
$f, $t, $tz
);
$ret = new self();
return $ret->setTimestamp($dateTime->getTimestamp());
}
Could I maybe use __callStatic
and return call_user_func_array(array(__CLASS__ , 'parent::'.__FUNCTION__), func_get_args());
or something?
Too bad I can't magically convert DateTime
to use late static bindings.