1

I have created same method printData() in the MyInterface.php and MyTrait.php and i am calling the method printData() from my controller which implements the MyInterface.php and use MyTrait.php. But the method is always calling from MyInterface.php. Please explain it why this occurs?

MyInterface.php

<?php namespace App\Interfaces;

interface MyInterface
{
    public function printData();
}

MyTrait.php

<?php namespace App\Traits;

trait MyTrait
{
    public function printData()
    {
        dd("From Trait");
    }
}

HomeController.php

<?php namespace App\Http\Controllers;

use App\Interfaces\MyInterface;
use App\Traits\MyTrait;
use App\User;

class HomeController extends Controller implements MyInterface
{
    use MyTrait;

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

    public function printData() {
        // TODO: Implement printData() method.
        dd("From Interface");
    }
}
4
  • 1
    There's no way this code would work normally. Chances are you need to add use App\Traits\MyTrait to see the result Commented Oct 30, 2017 at 12:38
  • @apokryfos yeah i missed that, i will update my question Commented Oct 30, 2017 at 12:39
  • 2
    Methods in a class always take precedence over a method imported from a trait. See the section on precedence in the manual. Commented Oct 30, 2017 at 12:40
  • Do you actually need both implementations in your class? The quickest fix for this is probably to just remove the printData method from your class, since they it will always use the version from the trait. It will still be considered to implement the interface, if that's a concern in other parts of your code. Commented Oct 30, 2017 at 12:59

2 Answers 2

6

Try using it like this:

class HomeController extends Controller implements MyInterface
{
    use MyTrait{
        printData as MyTraitPrintData;
    }

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

    public function printData()
    {

        dd("From Interface");
    }
}

Traits are meant to provide reusable methods and properties. They will not override the class's own methods. By defining printData() inside the HomeController you are actually overriding the printData() inside MyTrait. That is why you have to differentiate them from each other.

Learn more about all this in Horizontal Reuse for PHP

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

Comments

1

The method is not calling from MyInterface but from HomeController.

The reason is because HomeController acquires the method from the trait MyTrait but then overrides it.

If you want that method in your trait, you don't need to declare an interface.

What interfaces do is forcing the classes which implements it to declare those methods, but you don't want to do that since you're creating the method in a Trait..

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.