Re: RFC for Adding __toString to DateTime
| From: | Will Fitch | Date: | Sun, 02 Sep 2012 04:08:10 +0000 |
| Subject: | Re: RFC for Adding __toString to DateTime | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-62655@lists.php.net to get a copy of this message | ||
On Sat, Sep 1, 2012 at 4:54 PM, Derick Rethans <derick@php.net> wrote:
> On Sat, 1 Sep 2012, Will Fitch wrote:
>
> > I would like to officially introduce an RFC with a patch to implement
> > __toString to DateTime. This is a commonly requested feature that goes
> > unanswered mostly because of the inability to agree on a default pattern.
> >
> > In short, the patch uses the ISO-8601 format as the default pattern. The
> > pattern may be changed via setDefaultPattern and
> date_default_pattern_set,
> > as explained in the RFC.
>
> The default should most definitely not be ISO-8601 - as it's a lossy
> format.
>
In terms of lossy transmission/storage, what concerns in regards to the
pattern are you referring to? ISO-8601 is only output *if* there is no
pre-set pattern provided. So in essance,
>
> > The link to the RFC and patch are here:
> > https://wiki.php.net/rfc/datetime_tostring.
>
> > Methods Added
> >
> > void DateTime::setDefaultPattern(string $pattern)
> > string DateTime::getDefaultPattern(void)
>
> This should be descriptive. You are *only* dealing with __toString here.
> Hence the method names should reflect that:
>
> void DateTime::setToStringFormat(string $pattern)
> string DateTime::getToStringFormat(void)
> However, I still don't believe this belongs in the class at all. It is
> rather trivial to do this in userland:
>
> class MyDateTime extends DateTime {
> static $format = "... some format...";
> function __toString() { return $this->format(DateTime::$format); }
> }
>
> There, 4 lines. And no quaralling about formats or naming.
>
I'm definitely not sold on any naming convention or format, and I don't see
any quarrelling going on. The purpose of the RFC is to come up with a
solution and discuss implementation. I don't expect any quarrels for
something this simple.
I would like this discussed. This is a feature often brought up and
requested. I understand that it may be difficult to come to a solution,
but let's try.
>
> > <?php
> > $date = new DateTime('2012-09-01 02:43:00');
> > print_r($date);
>
> Should not output just:
>
> > DateTime Object
> > (
> > [date] => 2012-09-01 02:43:00
> > [timezone_type] => 3
> > [timezone] => America/Chicago
> > )
>
> But instead should always have the tostring pattern in there.
>
I agree and will update.
>
> Also, have you checked whether it shows up when you run serialize on it?
>
The patch I originally wrote did implement wakeup and set state.
Unfortunately, an accidental git checkout -- in the wrong path gave me the
distinct pleasure of rewriting this.
>
> cheers,
> Derick
>