【发布时间】:2020-12-14 14:39:37
【问题描述】:
我正在尝试扩展 DateTime,如下所示:
class testdate extends DateTime {
public $sqldate;
public function __construct($time)
{
parent::__construct($time);
//?? parent::modify();
$this->sqldate = $this->format ("Y-m-d");
}
}
echo "<pre>";
$td = new testdate("2020-08-23");
echo " Today's Date: ".$td->format ("m/d/Y").br;
echo " Today's SQL Date: ".$td->sqldate.br.br;
$td->modify ("+24 hour");
echo " Tomorrow;s Date: ".$td->format ("m/d/Y").br; // 1 day added correctly
echo " Tomorrow Formatted: ".$td->format ("Y-m-d").br;
echo " Tomorrow Sql Date: ".$td->sqldate.br.br; //not updated
print_r ($td);
正如您在 print_r 语句中看到的,日期已更新,但 sqldate 未更新。
我必须做些什么来确保扩展类的属性得到更新?
【问题讨论】:
-
您没有包含
print_r的结果。这将有助于显示这些回声的确切输出。 -
$this->sqldate在调用$td->modify()时不会更新。您还需要覆盖modify()方法。 -
或者只是让
sqldate成为一个方法,而不是一个属性。