brick / date-time

Date and time library for PHP
MIT License
326 stars 30 forks source link

Rewrite constructors to use "static"? #5

Closed hogacz closed 6 years ago

hogacz commented 6 years ago

http://php.net/manual/en/language.oop5.late-static-bindings.php

Is it possible to use this approach, so if one subclassed your class, constructors are returning also instances of the subclass instead of the original one? Otherwise, subclassing has limited usage. Example: I wanted to subclass LocalDate to add a very simple "format" method, that will just return "YYYY-mm-dd", but there might be other use cases.

BenMorel commented 6 years ago

Hi, sorry for the late reply.

I'm not fond of allowing subclassing in my libraries, unless some class is designed specifically for this purpose. Actually, I might even make the classes final at some point, for 2 reasons:

I'd rather then implement what you think is missing from the library (in this case, a custom format method), so I will close this issue as a duplicate of #3, which is asking for a custom format method as well.

As a final note, did you see that __toString() actually returns YYYY-mm-dd? So you should be able to use (string) $date and be set.