Re: RFC for Adding __toString to DateTime
| From: | Derick Rethans | Date: | Sat, 01 Sep 2012 20:54:51 +0000 |
| Subject: | Re: RFC for Adding __toString to DateTime | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-62636@lists.php.net to get a copy of this message | ||
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.
> 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.
> <?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.
Also, have you checked whether it shows up when you run serialize on it?
cheers,
Derick