vendredi 17 mai 2019

Laravel use Trait methods in another Trait

Normally I have a question about something not working, now I have a question about something that IS working, I am just confused as to why. This is the structure that I have in Laravel:

ExampleController

use App\Http\Traits\Trait1;
use App\Http\Traits\Trait2;

ExampleController extends Controller {

   use Trait1, Trait2;

   public function index()
   {
      // I can use methods from Trait1 and Trait2 here, works fine
   }

}

Trait1

namespace App\Http\Traits;

trait Trait1 {
   exampleMethodTrait1()
   {
   }
}

Trait2

namespace App\Http\Traits;

trait Trait2 {

   $test = $this->exampleMethodTrait1();

}

Calling a method defined in Trait1 from Trait2 actually works, while I have not added use App\Http\Traits\Trait1; in Trait2. Is that because they are both loaded in the controller?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire