how broker() function in trait SendsPasswordResetEmails return \Illuminate\Contracts\Auth\PasswordBroker?

laravel, laravel-5, laravel-5.1, laravel-5.3

Solution

The `Password` facade returns an instance of `Illuminate\Auth\Passwords\PasswordBrokerManager`.

Within `PasswordBrokerManager` there is a method called `broker`. It is the method `broker` that then returns an instance of `PasswordBroker`.

Basically, this:

Password::broker();

is just another way to write:

$manager = new PasswordBrokerManager();
return $manager->broker();

You are returning the results of that method not the method itself.

The way `Facades` work (in a nutshell).

Facades provide a way for you to get a class without having to `new` it up (they have a few other benefits as well but we don't need to go in to them). You'll notice that you will always call a method from a Facade statically, this is because there is a `magic method` in the facade called `__callStatic` which will be called if there isn't a method in that class with that name (and the method is called statically). This then uses the `getFacadeRoot` and `getFacadeAccessor` methods to find out what is actually meant to called. It then gets an instance of that call and calls this method on it (in this example `broker`).

If you go to `Illuminate\Auth\Passwords\PasswordResetServiceProvider` you'll see:

 $this->app->singleton('auth.password', function ($app) {
        return new PasswordBrokerManager($app);
    });

The above is telling `Laravel` to register `auth.password` as an instance `PasswordBrokerManager`. Then in the `Password` Facade class you'll see:

protected static function getFacadeAccessor()
{
    return 'auth.password';
}

Hope this helps!

Problem

I am using Laravel 5.3, In Forgot Password Controller, there is `Trait SendsPasswordResetEmails` if you go to it's definition, there is function called `broker()`, it returns `contract` of type `\Illuminate\Contracts\Auth\PasswordBroker` if you go to `\Illuminate\Contracts\Auth\PasswordBroker` , there is no function declaration with the name of `broker()` and not even in it's derived class `\Illuminate\Auth\Passwords\PasswordBroker.php` I saw it was present in `\Illuminate\Auth\Passwords\PasswordBrokerManager.php` Question: Can you kindly tell how `broker()` function in `trait SendsPasswordResetEmails` return `\Illuminate\Contracts\Auth\PasswordBroker` ?

Original source