Member Avatar for diafol

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?

What if you just call the parent constructor.

Member Avatar for diafol

How do you mean? Like in my first example, or a different method?

I've been playing with this most of the day and seem to keep getting the Exception that states that the object wasn't correctly initalized when I try different methods of extending. :)

I think the first method is the only one.

Member Avatar for diafol

OK, good to get an expert opinion. This has cooked my noodle. :) Thanks again.

I gave this some more thought, and the only way I can come up with, that is not using a constructor, is using a factory that will return a new instance of your class after setting some properties. Personally, I prefer the constructor, because that's what it's for.

Member Avatar for diafol

Ahh, didn't think of that. But I agree, the constructor seems a better option to me too. It's just the problem with DateTime methods that some parameters are objects created on the fly, e.g DateInterval, DateTimeZone etc, which don't work as subclass constructor parameters as the object hasn't been initialised yet. Doh. However, this can act to make cleaner client code:

$u = new diaDate('now','Europe/London');

is nicer to deal with than:

$u = new DateTime('now', new DateTimeZone('Europe/London'));

Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.