How to use traits in Laravel 5.4.18?

前端 未结 3 1629
清酒与你
清酒与你 2021-01-12 09:06

I need an example of where to exactly create the file, to write to it, and how to use the functions declared in the trait. I use Laravel Framework 5.4.18

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-12 09:36

    Let's look at an example trait:

    namespace App\Traits;
    
    trait SampleTrait
    {
        public function addTwoNumbers($a,$b)
        {
            $c=$a+$b;
            echo $c;
            dd($this)
        }
    }
    

    Then in another class, just import the trait and use the function with this as if that function was in the local scope of that class:

    addTwoNumbers(5,10);
        }
    }
    

提交回复
热议问题