Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide carbon-extended as trait/mixin #4

Open
kylekatarnls opened this issue Feb 20, 2020 · 1 comment
Open

Provide carbon-extended as trait/mixin #4

kylekatarnls opened this issue Feb 20, 2020 · 1 comment
Assignees
Labels
enhancement New feature or request features
Milestone

Comments

@kylekatarnls
Copy link

kylekatarnls commented Feb 20, 2020

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:

trait BeerDayCarbonTrait
{
    public function nextBeerDay()
    {
        return $this->modify('next Wednesday');
    }

    public function previousBeerDay()
    {
        return $this->modify('previous Wednesday');
    }
}

Carbon::mixin(BeerDayCarbonTrait::class);

$date = Carbon::parse('First saturday of December 2018');

echo $date->previousBeerDay();                                                 // 2018-11-28 00:00:00
echo "\n";
echo $date->nextBeerDay();                                                     // 2018-12-05 00:00:00

Try on try-carbon.herokuapp.com

And this is a backward-compatible change as you can do:

<?php

namespace Lee;

use Carbon\Carbon;

// Optionally you can deprecate it for next version
@trigger_error('CarbonExtended class is deprecated and will be removed in carbon-extended 2, use Carbon::mixin(CarbonExtendedTrait::class) instead.', E_USER_DEPRECATED);

class CarbonExtended extends Carbon
{
  use CarbonExtendedTrait;
}

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

@peter279k peter279k self-assigned this Feb 20, 2020
@peter279k peter279k added enhancement New feature or request features labels Feb 20, 2020
@peter279k
Copy link
Owner

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 :).

@peter279k peter279k added this to the v2.0 milestone Feb 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request features
Projects
None yet
Development

No branches or pull requests

2 participants