miwal

Overriding a method implemented in a (used) trait

Posted 9 months ago by miwal

The trait Illuminate\Foundation\Auth\RegistersUsers contains the following method (I copy it exactly):

    /**
     * The user has been registered.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  mixed  $user
     * @return mixed
     */
    protected function registered(Request $request, $user)
    {
        //
    }

The trait is used in RegisterController, in Http/Controllers/Auth

As above the trait contains the above method with an empty method body. Obviously we don't want to change this trait in the framework code in vendor/.

In a video, what Jeff did was implement the method in RegisterController.

I just wanted to get something clear: a trait is a way of doing multiple inheritance, it's obviously not an interface.

In php, if a class uses a trait, and it implements a method that is also implemented in the (multiple inheritance...) trait, do we know for a fact that the method body in the class itself will take precedence over the same method in the trait?

Is that the behaviour being relied upon here?

Jeff doing the above is at 1:20 here: https://laracasts.com/series/lets-build-a-forum-with-laravel/episodes/73

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.