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.
Like you already said & tried, override static method. Method createFromFormat
by default returns the DateTime
object, so you only need to fix returning part so it will return your object SerialDateTime
instead of DateTime
.
class SerialDateTime extends DateTime implements JsonSerializable {
public function jsonSerialize()
{
return ['timestamp' => $this->getTimestamp()];
}
public static function createFromFormat($format, $time, $timezone = null)
{
if ($timezone) {
$dt = parent::createFromFormat($format, $time, $timezone);
} else {
$dt = parent::createFromFormat($format, $time);
}
return new self($dt->format(self::W3C));
}
}
echo json_encode(new SerialDateTime);
echo json_encode(SerialDateTime::createFromFormat('Y', '2013'));
It doesn't matter how you call static method createFromFormat
, it will always return DateTime
object; so all your ideas of automatically rewriting static methods will fail, because you need to modify method with new logic (return instance of other object), and this cannot be accomplished with auto-call-method-magic-or-something.
Late static bindings would be great if it were implemented in the DateTime::createFromFormat
method like :
public static function createFromFormat($format, $time, $timezone = null)
{
// logic of converting $time from format $format to some default format
return new static($newTime);
}
... but it is not ;( Source code
So, I will post my answer here.
In my opinion overriding static function createFromFormat
is the best way to deal with your problem.
Because:
call_user_func
)SerialDateTime
will be futher reusable. (if you want to import only class code)It is not necessary though to override all methods (unless you implement an interface). Override just only those you need.