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");
}
}
use App\Traits\MyTraitto see the resultprintDatamethod 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.