Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使date_create()与DateTime子类一起工作_Php_Oop_Datetime - Fatal编程技术网

Php 使date_create()与DateTime子类一起工作

Php 使date_create()与DateTime子类一起工作,php,oop,datetime,Php,Oop,Datetime,php的DateTime类的一个恼人之处是它没有\uu toString()方法,这意味着每当您试图在字符串上下文中使用它时,它都会抛出一个错误。所以我扩展了它: class ZDateTime extends DateTime { private $zformat='Y-m-d H:i:s'; function __toString() { return $this->format($this->zformat); } func

php的DateTime类的一个恼人之处是它没有
\uu toString()
方法,这意味着每当您试图在字符串上下文中使用它时,它都会抛出一个错误。所以我扩展了它:

class ZDateTime extends DateTime {

    private $zformat='Y-m-d H:i:s';

    function __toString() {
        return $this->format($this->zformat);
    }

    function set_format($format){
        $this->zformat=$format; //leaving out format validation for brevity
    }
}
但是现在我已经丢失了DateTime所拥有的所有漂亮的过程函数,如
date\u create()
和列出的许多其他函数

我知道过程函数只是
DateTime
类方法的别名。但它们通常更易于使用,并且在我的代码中大量使用。把它们捞出来会很痛苦。那么,你知道如何让
date\u create()
返回
ZDateTime
对象吗

那么,知道如何让date_create()返回ZDateTime对象吗

你不能那样做。除了极少数例外(PDO),返回新实例的内置类和函数无法返回子类

考虑向ZDateTime类添加一个新的静态方法,该方法执行相同的操作:创建对象,但返回
false
,而不是在传递的字符串不可解析时引发异常。您只需要捕获函数内部的异常。

date\u create()
DateTime
构造函数的别名。因此,只需创建一个新的
ZDateTime
实例

如果你发现自己有一个
DateTime
对象,你可以在课堂上这样做:

public static function fromDateTime(DateTime $foo)
{
  return new static($foo->format('Y-m-d H:i:s e')); 
}

$foo = ZDateTime::fromDateTime($dt);
然后可以扩展一些静态方法,如:

public static function createFromFormat($f, $t, $tz)
{
  return static::fromDateTime(parent::createFromFormat($f, $t, $tz));
}

$dt = ZDateTime::createFromFormat(...);

最简单的方法可能只是创建一个
zdate\u create
函数并进行大规模搜索和替换

这是可以做到的,但在vanilla PHP中(即:不使用runkit等)做到这一点的唯一方法是:

ZDateTime.php

namespace MyDateTime;

class ZDateTime extends \DateTime {
    // do your stuff
}

function date_create($time = 'now', \DateTimeZone $timezone = null) {
    if (isset($timezone)) {
        return new ZDateTime($time, $timezone);
    } else {
        return new ZDateTime($time);
    }
}
namespace MyDateTime;
include 'ZDateTime.php';

$foo = date_create();
var_dump($foo); // ZDateTime object
Some_Script.php

namespace MyDateTime;

class ZDateTime extends \DateTime {
    // do your stuff
}

function date_create($time = 'now', \DateTimeZone $timezone = null) {
    if (isset($timezone)) {
        return new ZDateTime($time, $timezone);
    } else {
        return new ZDateTime($time);
    }
}
namespace MyDateTime;
include 'ZDateTime.php';

$foo = date_create();
var_dump($foo); // ZDateTime object

我同意搜索和替换评论。我认为使用
名称空间
以不兼容的方式重写核心PHP函数有点滥用。没有理由不按正确的方式做。@konforce:我不认为这是一种虐待。这是名称空间的一个优点。使用名称空间的一个要点是能够定义已经定义的符号。我当然认为搜索和替换更好,但要回答OP的问题,是的,它是可以做到的,唯一的方法是使用名称空间。我称之为滥用,因为它是对一个预先存在的问题的一种掩饰。也就是说,如果你重新开始,这不是解决这个问题的自然方法。在date对象上引入名称空间来获得免费的
\uuu toString()
,这对我来说太过分了。聪明的解决方案是+1。我想我同意,搜索和替换,除去其中的烦恼,是更好的方式。谢谢。虽然手册坚持它是构造函数的别名,但它的行为不同。当传递的字符串不可解析时,它不会引发异常,而是简单地返回false。