Open kylekatarnls opened 4 years ago
Hi @kylekatarnls, thanks for your reply.
Thanks for your sharing the Carbon::mixin
method :) and it will be useful for someone use many other Carbon extended packages conveniently!
I will do this on my next release 2.x
version :).
Hello,
Classes that extend Carbon like jensseggers/date or this one have one major inconvenient. You have to choose one or the other. You can't benefit of both methods of 2 or more classes that extend a first one (Carbon in this case).
A more extensible approach is to use traits and Carbon provides out of the box the
mixin()
method which can take as much traits as you want as long as they don't use the same methods names:[Try on try-carbon.herokuapp.com](https://try-carbon.herokuapp.com/?hide-output-gutter&output-left-padding=10&theme=tomorrow_night&border=none&radius=4&v-padding=15&input=trait%20BeerDayCarbonTrait%0A%7B%0A%20%20%20%20public%20function%20nextBeerDay()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20return%20%24this-%3Emodify(%27next%20wednesday%27)%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20public%20function%20previousBeerDay()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20return%20%24this-%3Emodify(%27previous%20wednesday%27)%3B%0A%20%20%20%20%7D%0A%7D%0A%0ACarbon%3A%3Amixin(BeerDayCarbonTrait%3A%3Aclass)%3B%0A%0A%24date%20%3D%20Carbon%3A%3Aparse(%27First%20saturday%20of%20December%202018%27)%3B%0A%0Aecho%20%24date-%3EpreviousBeerDay()%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%202018-11-28%2000%3A00%3A00%0Aecho%20%22%5Cn%22%3B%0Aecho%20%24date-%3EnextBeerDay()%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%202018-12-05%2000%3A00%3A00%0A&token=live-editor-158)
And this is a backward-compatible change as you can do:
And Carbon documentation has a macro section where such mixins/macros can be promoted if you want by submitting a pull-request on gh-pages branch on https://github.com/briannesbitt/Carbon