duandaodao6951 2013-09-11 16:02
浏览 31
已采纳

从父的静态方法返回子类

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.

  • 写回答

2条回答 默认 最新

  • duanqun7761 2013-09-12 13:36
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答