Re: DateTimeInterface is not a usable interface?
| From: | Niktia Nefedov | Date: | Tue, 21 Jul 2015 10:05:54 +0000 |
| Subject: | Re: DateTimeInterface is not a usable interface? | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-87218@lists.php.net to get a copy of this message | ||
On Tue, 21 Jul 2015 15:01:06 +0400, Marco Pivetta <ocramius@gmail.com> wrote:
Hello, I was looking at DateTimeInterface in order to provide my own implementation of it, when I hit this: http://3v4l.org/8GvgO "Fatal error: DateTimeInterface can't be implemented by user classes in" Is there any actual reason for this kind of limitation? Can it be removed before going stable? I also wasn't able to find any RFC information around it on https://wiki.php.net/rfc#php_55 - is there any previous discussion around this, or did it just sneak its way in? Greets, Marco Pivetta http://twitter.com/Ocramius http://ocramius.github.com/Hey Marco, The discussion was here https://github.com/php/php-src/pull/512 tl;dr core functions that accept DateTime and DateTimeImmutable (DateTimeInterface basically) operate directly on timelib structures hence they will break if you pass them a user-defined class. There's a workaround though - it's to always call user-defined class's method directly from built-in functions, so if you would call (new DateTime())->diff(new UserDefinedDateTime()) it would call UserDefinedDateTime#diff in the end which would need to implement the logic.