0

I have a MyTranslatable trait, which uses another OtherTranslatable trait, the one from a third party package. I'm including them as follows.

trait MyTranslatable
{
    use OtherTranslatable {
        OtherTranslatable::method as public otherTranslatableMethod;
    }

    public function index()
    {
        // Perform my actions

        // Call otherTranslatableMethod
        static::otherTranslatableMethod();
    }

I'm wondering if this is valid, the code is working but I'm not so sure if traits are meant to be used this way, and if this is the correct way to call the "parent trait's" method like this after my actions are complete

2
  • Sure, that's valid. Commented Apr 15, 2020 at 17:43
  • It may be valid, whether it is appropriate Is disputable, but theres not enough context to tell. But probably not, traits are rarely appropriate. Traits mean multiple inheritance, multiple inheritance means violation of SRP... Commented Apr 16, 2020 at 3:30

1 Answer 1

1

I think your code from architectural point of view is correct since child blueprints (classes, traits) in PHP can override in use statement of the context, the methods from already used traits (I avoid call this inheritance because using trait is actually copy-paste), but I don't find quite necessary to invoke your otherTranslatableMethod() statically in MyTranslatable (of course except if it's defined as static in OtherTranslatable trait).

If you have non-static definition of OtherTranslatable->method() you can do the following in order to make your code more correct and clean.

trait MyTranslatable
{
    use OtherTranslatable {
        method as public otherTranslatableMethod;
    }

    public function index()
    {
        // Perform my actions
        // Call otherTranslatableMethod
        $this->otherTranslatableMethod();
    }
}

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.