Hi, I've been playing about with native classes and looking to extend them - well just the DateTime class actually. I was hpong that there would be a way to avoid using a constructor, but I can't seem to figure out how to store a 'startup' timestamp, u
without it. I'm assuming this property is essential for the reset()
method.
Everything seems to work OK, as long as I supply a valid TimeZone.
class diaDate extends DateTime{
private $u;
public function __construct($time='now', $timezone='Europe/London')
{
parent::__construct($time, new DateTimeZone($timezone));
$this->u = $this->format('U');
}
public function __toString()
{
return $this->format('Y-m-d H:i:s');
}
public function reset()
{
$this->setTimestamp($this->u);
}
}
$u = new diaDate('2012-10-22');
echo $u->format('r e') . "<br />";
$u->modify('+3 days');
echo $u->format('r e') . "<br />";
$u->reset();
echo $u->format('r e') . "<br />";
However, taking the construct out:
class diaDate extends DateTime{
//private $u;
public function __toString()
{
return $this->format('Y-m-d H:i:s');
}
public function reset()
{
//$this->setTimestamp($this->u);
}
}
$u = new diaDate('2012-10-22',new DateTimeZone('Europe/London'));
echo $u->format('r e') . "<br />";
$u->modify('+3 days');
echo $u->format('r e') . "<br />";
$u->reset();
echo $u->format('r e') . "<br />";
The above works, but obviously not the reset, as it's not possible to store the original datetime.
Any ideas about this? Can I make a extended class with functions like reset() without using __construct?