What is the Laravel trait pattern?
<p>While looking at the Laravel source, I noticed a lot of stuff like this:</p>

<p><em>A controller class:</em></p>

<pre class="lang-php prettyprint-override"><code>class Controller extends BaseController
{
    use AuthorizesRequests, AuthorizesResources, DispatchesJobs, ValidatesRequests;
}
</code></pre>

<p><em>One of its component traits:</em></p>

<pre class="lang-php prettyprint-override"><code>trait AuthorizesRequests {

    /**
     * Authorize a given action against a set of arguments.
     *
     * @param  mixed $ability
     * @param  mixed|array $arguments
     *
     * @return \Illuminate\Auth\Access\Response
     *
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function authorize($ability, $arguments = []) {
        list($ability, $arguments) = $this-&gt;parseAbilityAndArguments($ability, $arguments);
        return app(Gate::class)-&gt;authorize($ability, $arguments);
    }

    // ...

}
</code></pre>

<p>I have a couple of questions about this:</p>

<ul>
<li>Does this pattern (abstracting re-usable functionality into traits) have a name?</li>
<li>Is this pattern used to good effect in any other projects?</li>
<li>If a trait requires dependencies, is there a best-practise way to inject them, instead of using a service locator (like <code>app()</code>, in this case)?</li>
</ul>

<p>I'm considering using this approach in my code to share some general functionality between a couple of my classes - I'm considering creating a <code>ChecksBarcodes</code> trait, which will work with a repository of stock information, and sharing that trait between a few similar but unrelated product management process classes, which all need to check barcodes.</p>