php - Make DateTime::createFromFormat() return child class instead of parent -


I'm expanding DateTime to add some useful methods and constants < P> Everything is okay when using the new to create a new object, but when using the static method createFromFormat it always returns the original DateTime object And certainly none of the hair methods are available.

Am I using the following code to solve this problem? Is this the best way?

  Name space noilibbs \ datetime; Class datetime extension \ datetime {static public function createFromFormat ($ format, $ time) {$ ext_dt = new self (); $ Ext_dt- & gt; Settimestamp (guardian :: createFromFormat ($ format, time) -> getTimestamp ()); Return $ ext_dt; }}    

This is a way to go though, because you think what you do Want to provide Extensible DateTime class, I suggest you use static instead of self :

  Namespace NoiseLabs \ DateTime; Class Datetime extension \ datetime {stable public function createFromFormat ($ format, $ time) is {$ ext_dt = new static (); $ Ext_dt- & gt; Settimestamp (guardian :: createFromFormat ($ format, $ time) -> getTimestamp ()); Return $ ext_dt; }}   

If you do not plan to expand the class, then it is not necessary, but if anyone does, then it will prevent them from repeating the same action.

Comments

Popular posts from this blog

qt - switch/case statement in C++ with a QString type -

python - sqlite3.OperationalError: near "REFERENCES": syntax error - foreign key creating -

Python's equivalent for Ruby's define_method? -