Search code examples
phplaravelrevisionable

How can I have my trait extend a class


I understand that by definition, a trait cannot extend a class, however I'm wondering if there's some kind of workaround.

My situation:

I created the package revisionable, which in its first incarnation was a class that you extended from your model which itself extended Laravels base Eloquent class, however over time there were plenty of requests to change this into a trait, so people could use revisionable, and be able to extend their own base class.

Currently, the only that I can think to allow for both an extendable class, and a trait, is to offer both as a whole files, which means I'm repeating the entire code in two files which could easily lead to trouble down the road.

I'm wondering if anybody knows of some solution where I can have one file that is god, and the other file relies on it.

Rules

  • The existing class cannot use a trait, as existing users of the package on php 5.3 will not have access to traits.

Solution

  • There's absolutely no way to accomplish what you want.


    Since Laravel itself (in 4.2) has now abandoned PHP 5.3, it's time to move on too.

    Tag a new release that drops the class, add a PHP 5.4 requirement to your composer.josn file, and add this information to your docs.

    Anyone still stuck on 5.3 can always just composer require your previous version.