11

I have this specific situation, my trait has a method and my class has a method, both with same name.

I need to use both methods (the one from the trait and the class) Inside that class which contains that same method

namespace Some\Namespace;
use Some\Other\Namespace\TestTrait;

class TestClass {

  use TestTrait;

  public function index()
  {
    // should call the method from the class $this->getId();
    // should also call the method from the trait $this->getId();
  }

  private function getId()
  {
    // some code
  }
}

And in seperate defined Trait:

trait TestTrait
{
    private function getId ()
    {
        // does something
    }
}

Please note this is not pasted code, I might have some typos :P

0

1 Answer 1

21

Use trait Conflict Resolution

namespace Some\Namespace;
use Some\Other\Namespace\TestTrait;

class TestClass {

  use TestTrait {
      getId as traitGetId;
  }

  public function index()
  {
    $this->getId();
    $this->traitGetId();
  }

  private function getId()
  {
    // some code
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer, (sorry for late reply) I guess this will work, mind that I still will want to test this in my code, just to be 100% sure :)

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.