# service container in laravel with example

<div class="WaaZC" id="bkmrk-he-laravel-service-c" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);"><div class="rPeykc" data-hveid="CAYQAQ" data-ved="2ahUKEwi2ko_Qnf2QAxUqRCoJHTDIOjAQo_EKegQIBhAB" style="margin: 0px 0px 10px;"><span data-huuid="13784814277509726051">he Laravel service container, also known as the Inversion of Control (IoC) container, is a powerful tool for managing class dependencies and performing dependency injection. </span><span data-huuid="13784814277509725202">It acts as a central registry for your application's classes, allowing Laravel to automatically resolve and inject required dependencies when needed.<span class="pjBG2e" data-cid="73e7cebf-b785-4c45-a9fa-cb21ef75aead"><span class="UV3uM" style="white-space: nowrap;"> </span></span></span><div class="NPrrbc" data-cid="73e7cebf-b785-4c45-a9fa-cb21ef75aead" data-uuids="13784814277509726051,13784814277509725202" style="margin-inline-end: 6px; vertical-align: middle; display: inline-flex;"><div aria-label="View related links" class="BMebGe btku5b LwdV0e FR7ZSc OJeuxf" data-hveid="CAUQAQ" data-ved="2ahUKEwi2ko_Qnf2QAxUqRCoJHTDIOjAQ3fYKegQIBRAB" jsaction="KjsqPd" jsname="HtgYJd" role="button" style="display: inline-block; vertical-align: middle; cursor: pointer; outline: 0px; -webkit-tap-highlight-color: transparent; color: rgb(28, 65, 154);" tabindex="0"><div class="niO4u" style="display: flex; justify-content: center; position: relative; align-items: center; width: 28px; z-index: 0; box-sizing: border-box; border-radius: 9999px; padding: 0px; border: 1px solid transparent; background: transparent; height: 20px; margin: 0px auto; min-height: 20px;"><span class="TwMgNb Hkv2Pe" style="color: rgb(31, 31, 31) !important; background: unset !important; display: flex; align-items: center;"><span class="iPjmzb Sorfoc gNGSDf" style="display: flex; height: unset; rotate: 135deg;"><span class="z1asCe Sb7k4e" style="display: inline-block; fill: currentcolor; height: 18px; line-height: 18px; position: relative; width: 18px;"><svg aria-hidden="true" focusable="false" viewbox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M3.9 12c0-1.71 1.39-3.1 3.1-3.1h4V7H7c-2.76 0-5 2.24-5 5s2.24 5 5 5h4v-1.9H7c-1.71 0-3.1-1.39-3.1-3.1zM8 13h8v-2H8v2zm9-6h-4v1.9h4c1.71 0 3.1 1.39 3.1 3.1s-1.39 3.1-3.1 3.1h-4V17h4c2.76 0 5-2.24 5-5s-2.24-5-5-5z"></path></svg></span></span></span></div></div></div></div></div></div><div class="WaaZC" id="bkmrk-key-concepts%3A" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);"><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CAIQAQ" data-ved="2ahUKEwi2ko_Qnf2QAxUqRCoJHTDIOjAQo_EKegQIAhAB" role="heading" style="margin: 20px 0px 10px; font-size: 20px; font-weight: 600; line-height: 28px;"><span data-huuid="13784814277509723504">Key Concepts:</span></div></div></div><div class="WaaZC" id="bkmrk-binding%3A%C2%A0-registerin" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);">- <div class="zMgcWd dSKvsb" data-il="" style="padding-bottom: 0px; padding-top: 0px; border-bottom: none;"><div data-crb-p=""><div class="xFTqob" style="flex: 1 1 0%; min-width: 0px;"><div class="Gur8Ad" style="font-size: 16px; font-weight: 500; line-height: 24px; overflow: hidden; padding-bottom: 4px; transition: transform 200ms cubic-bezier(0.2, 0, 0, 1); display: inline;"><span data-huuid="13784814277509725053">**Binding:** </span></div><div class="vM0jzc" style="color: rgb(10, 10, 10); font-size: 16px; line-height: 24px; display: inline;"><span data-huuid="13784814277509724204">Registering a class or an interface and its implementation with the container. </span><span data-huuid="13784814277509723355">This tells the container how to build an instance of that class or what implementation to use for a given interface.</span></div></div></div></div>
- <div class="zMgcWd dSKvsb" data-il="" style="padding-bottom: 0px; padding-top: 8px; border-bottom: none;"><div data-crb-p=""><div class="xFTqob" style="flex: 1 1 0%; min-width: 0px;"><div class="Gur8Ad" style="font-size: 16px; font-weight: 500; line-height: 24px; overflow: hidden; padding-bottom: 4px; transition: transform 200ms cubic-bezier(0.2, 0, 0, 1); display: inline;"><span data-huuid="13784814277509725753">**Resolving:** </span></div><div class="vM0jzc" style="color: rgb(10, 10, 10); font-size: 16px; line-height: 24px; display: inline;"><span data-huuid="13784814277509724904">Retrieving an instance of a registered class or implementation from the container. </span><span data-huuid="13784814277509724055">The container handles the creation of the object and its dependencies.</span></div></div></div></div>
- <div class="zMgcWd dSKvsb" data-il="" style="padding-bottom: 0px; padding-top: 8px; border-bottom: none;"><div data-crb-p=""><div class="xFTqob" style="flex: 1 1 0%; min-width: 0px;"><div class="Gur8Ad" style="font-size: 16px; font-weight: 500; line-height: 24px; overflow: hidden; padding-bottom: 4px; transition: transform 200ms cubic-bezier(0.2, 0, 0, 1); display: inline;"><span data-huuid="13784814277509722357">**Dependency Injection:** </span></div><div class="vM0jzc" style="color: rgb(10, 10, 10); font-size: 16px; line-height: 24px; display: inline;"><span data-huuid="13784814277509725604">The process of providing a class with its dependencies through its constructor or setter methods, rather than the class creating them itself. </span><span data-huuid="13784814277509724755">The service container automates this.</span></div></div></div></div>

</div></div><div class="WaaZC" id="bkmrk-example%3A" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);"><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CBwQAQ" data-ved="2ahUKEwi2ko_Qnf2QAxUqRCoJHTDIOjAQo_EKegQIHBAB" role="heading" style="margin: 20px 0px 10px; font-size: 20px; font-weight: 600; line-height: 28px;"><span data-huuid="4328967432750399371">Example:</span></div></div></div><div class="WaaZC" id="bkmrk-consider-a-scenario-" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);"><div class="rPeykc" data-hveid="CBsQAQ" data-ved="2ahUKEwi2ko_Qnf2QAxUqRCoJHTDIOjAQo_EKegQIGxAB" style="margin: 10px 0px;"><span data-huuid="4328967432750397669">Consider a scenario where you have a `NotificationService` responsible for sending notifications and a `UserService` that utilizes this service.</span></div></div></div><div class="WaaZC" id="bkmrk-code" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);"><div jsaction="rcuQ6b:npT2md" jscontroller="JegcYe"><div class="ecCNFc" style="border-image: none 100% / 1 / 0 stretch; border-radius: 8px; font-size: 14px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(245, 248, 255, 0.5); margin-bottom: 20px; color: rgb(10, 10, 10); border: 1.2px solid rgb(169, 172, 170);"><div class="zYSUYd" style="display: flex; flex-direction: row; overflow: hidden; border-radius: 8px 8px 0px 0px;"><div class="FS7GEb" style="display: flex; flex-direction: column; flex-grow: 1; min-width: 38%; width: 629.381px;"><div class="dDrxod" style="height: 34px; display: flex; justify-content: space-between; align-items: center; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(229, 237, 255); border-bottom: 1.2px solid rgb(169, 172, 170);"><div class="x7ndcb" style="padding-left: 16px; display: flex; align-items: center; color: rgb(10, 10, 10);">Code</div><div aria-live="polite" class="WDoJJe" style="margin-right: 4px; display: flex; align-items: center;"><button aria-label="Copy code to clipboard" class="hqI3tf B4zsNc" data-ved="2ahUKEwi2ko_Qnf2QAxUqRCoJHTDIOjAQ9MILegQIHxAB" role="button" style="margin: 0px 2px; background: none; height: 34px; border: none; color: rgb(5, 40, 97); position: relative; top: 2px; outline: 0px;" tabindex="0" title="Copy"><span class="z1asCe wm4nBd" style="display: inline-block; fill: currentcolor; height: 20px; line-height: 20px; position: relative; width: 20px;"><svg aria-hidden="true" enable-background="new 0 0 24 24" focusable="false" height="24" viewbox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><g><rect fill="none" height="24" width="24"></rect></g><g><path d="M16,20H5V6H3v14c0,1.1,0.9,2,2,2h11V20z M20,16V4c0-1.1-0.9-2-2-2H9C7.9,2,7,2.9,7,4v12c0,1.1,0.9,2,2,2h9 C19.1,18,20,17.1,20,16z M18,16H9V4h9V16z"></path></g></svg></span></button></div></div></div></div><div class="QQjpRc" style="display: flex; flex-direction: row; overflow: hidden; border-radius: 0px 0px 8px 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(245, 248, 255, 0.5);"><div class="FS7GEb" jsname="zEXxAd" style="display: flex; flex-direction: column; flex-grow: 1; min-width: 38%; width: 629.381px;"></div></div></div></div></div></div>```
// app/Services/NotificationService.php
namespace App\Services;

class NotificationService
{
    public function send(string $message): string
    {
        return "Sending Notification: " . $message;
    }
}

// app/Services/UserService.php
namespace App\Services;

class UserService
{
    protected NotificationService $notificationService;

    // The NotificationService is injected via the constructor
    public function __construct(NotificationService $notificationService)
    {
        $this->notificationService = $notificationService;
    }

    public function notifyUser(string $message): string
    {
        return $this->notificationService->send($message);
    }
}
```

<div class="WaaZC" id="bkmrk-using-the-service-co" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);"><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CCIQAQ" data-ved="2ahUKEwi2ko_Qnf2QAxUqRCoJHTDIOjAQo_EKegQIIhAB" role="heading" style="margin: 20px 0px 10px; font-size: 20px; font-weight: 600; line-height: 28px;"><span data-huuid="11094369998516857086">Using the Service Container:</span></div></div></div><div class="WaaZC" id="bkmrk-automatic-resolution" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);">- <span data-huuid="11094369998516855637">**Automatic Resolution in Controllers/Jobs:** </span><span data-huuid="11094369998516855154">Laravel automatically resolves dependencies for constructors in controllers, jobs, and other framework-managed classes.</span>

</div></div><div class="WaaZC" id="bkmrk-code-1" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);"><div jsaction="rcuQ6b:npT2md" jscontroller="JegcYe"><div class="ecCNFc" style="border-image: none 100% / 1 / 0 stretch; border-radius: 8px; font-size: 14px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(245, 248, 255, 0.5); margin-bottom: 20px; color: rgb(10, 10, 10); border: 1.2px solid rgb(169, 172, 170);"><div class="zYSUYd" style="display: flex; flex-direction: row; overflow: hidden; border-radius: 8px 8px 0px 0px;"><div class="FS7GEb" style="display: flex; flex-direction: column; flex-grow: 1; min-width: 38%; width: 629.381px;"><div class="dDrxod" style="height: 34px; display: flex; justify-content: space-between; align-items: center; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(229, 237, 255); border-bottom: 1.2px solid rgb(169, 172, 170);"><div class="x7ndcb" style="padding-left: 16px; display: flex; align-items: center; color: rgb(10, 10, 10);">Code</div><div aria-live="polite" class="WDoJJe" style="margin-right: 4px; display: flex; align-items: center;"><button aria-label="Copy code to clipboard" class="hqI3tf B4zsNc" data-ved="2ahUKEwi2ko_Qnf2QAxUqRCoJHTDIOjAQ9MILegQIKRAB" role="button" style="margin: 0px 2px; background: none; height: 34px; border: none; color: rgb(5, 40, 97); position: relative; top: 2px; outline: 0px;" tabindex="0" title="Copy"><span class="z1asCe wm4nBd" style="display: inline-block; fill: currentcolor; height: 20px; line-height: 20px; position: relative; width: 20px;"><svg aria-hidden="true" enable-background="new 0 0 24 24" focusable="false" height="24" viewbox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><g><rect fill="none" height="24" width="24"></rect></g><g><path d="M16,20H5V6H3v14c0,1.1,0.9,2,2,2h11V20z M20,16V4c0-1.1-0.9-2-2-2H9C7.9,2,7,2.9,7,4v12c0,1.1,0.9,2,2,2h9 C19.1,18,20,17.1,20,16z M18,16H9V4h9V16z"></path></g></svg></span></button></div></div></div></div><div class="QQjpRc" style="display: flex; flex-direction: row; overflow: hidden; border-radius: 0px 0px 8px 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(245, 248, 255, 0.5);"><div class="FS7GEb" jsname="zEXxAd" style="display: flex; flex-direction: column; flex-grow: 1; min-width: 38%; width: 629.381px;"></div></div></div></div></div></div>```
    // app/Http/Controllers/UserController.php
    namespace App\Http\Controllers;

    use App\Services\UserService;
    use Illuminate\Http\Request;

    class UserController extends Controller
    {
        public function __construct(protected UserService $userService)
        {
            // The UserService instance is automatically injected here
        }

        public function showNotifications()
        {
            $message = $this->userService->notifyUser('Welcome to our application!');
            return view('notifications', compact('message'));
        }
    }
```

<div class="WaaZC" id="bkmrk-manual-resolution%3A%C2%A0y" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);">- <span data-huuid="4328967432750399212">**Manual Resolution:** </span><span data-huuid="4328967432750398361">You can also explicitly resolve instances from the container using the `app()` helper or `App::make()`.</span>

</div></div><div class="WaaZC" id="bkmrk-code-2" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);"><div jsaction="rcuQ6b:npT2md" jscontroller="JegcYe"><div class="ecCNFc" style="border-image: none 100% / 1 / 0 stretch; border-radius: 8px; font-size: 14px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(245, 248, 255, 0.5); margin-bottom: 20px; color: rgb(10, 10, 10); border: 1.2px solid rgb(169, 172, 170);"><div class="zYSUYd" style="display: flex; flex-direction: row; overflow: hidden; border-radius: 8px 8px 0px 0px;"><div class="FS7GEb" style="display: flex; flex-direction: column; flex-grow: 1; min-width: 38%; width: 629.381px;"><div class="dDrxod" style="height: 34px; display: flex; justify-content: space-between; align-items: center; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(229, 237, 255); border-bottom: 1.2px solid rgb(169, 172, 170);"><div class="x7ndcb" style="padding-left: 16px; display: flex; align-items: center; color: rgb(10, 10, 10);">Code</div><div aria-live="polite" class="WDoJJe" style="margin-right: 4px; display: flex; align-items: center;"><button aria-label="Copy code to clipboard" class="hqI3tf B4zsNc" data-ved="2ahUKEwi2ko_Qnf2QAxUqRCoJHTDIOjAQ9MILegQIMBAB" role="button" style="margin: 0px 2px; background: none; height: 34px; border: none; color: rgb(5, 40, 97); position: relative; top: 2px; outline: 0px;" tabindex="0" title="Copy"><span class="z1asCe wm4nBd" style="display: inline-block; fill: currentcolor; height: 20px; line-height: 20px; position: relative; width: 20px;"><svg aria-hidden="true" enable-background="new 0 0 24 24" focusable="false" height="24" viewbox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><g><rect fill="none" height="24" width="24"></rect></g><g><path d="M16,20H5V6H3v14c0,1.1,0.9,2,2,2h11V20z M20,16V4c0-1.1-0.9-2-2-2H9C7.9,2,7,2.9,7,4v12c0,1.1,0.9,2,2,2h9 C19.1,18,20,17.1,20,16z M18,16H9V4h9V16z"></path></g></svg></span></button></div></div></div></div><div class="QQjpRc" style="display: flex; flex-direction: row; overflow: hidden; border-radius: 0px 0px 8px 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(245, 248, 255, 0.5);"><div class="FS7GEb" jsname="zEXxAd" style="display: flex; flex-direction: column; flex-grow: 1; min-width: 38%; width: 629.381px;"></div></div></div></div></div></div>```
    use App\Services\UserService;

    // ...

    $userService = app()->make(UserService::class);
    $message = $userService->notifyUser('Manual notification!');
```

<div class="WaaZC" id="bkmrk-binding-interfaces-t" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);"><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CDIQAQ" data-ved="2ahUKEwi2ko_Qnf2QAxUqRCoJHTDIOjAQo_EKegQIMhAB" role="heading" style="margin: 20px 0px 10px; font-size: 20px; font-weight: 600; line-height: 28px;"><span data-huuid="9189343244488997486">Binding Interfaces to Implementations (Advanced):</span></div></div></div><div class="WaaZC" id="bkmrk-this-is-particularly" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);"><div class="rPeykc" data-hveid="CDUQAQ" data-ved="2ahUKEwi2ko_Qnf2QAxUqRCoJHTDIOjAQo_EKegQINRAB" style="margin: 10px 0px;"><span data-huuid="9189343244488998112">This is particularly useful for testability and flexibility.</span></div></div></div><div class="WaaZC" id="bkmrk-code-3" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);"><div jsaction="rcuQ6b:npT2md" jscontroller="JegcYe"><div class="ecCNFc" style="border-image: none 100% / 1 / 0 stretch; border-radius: 8px; font-size: 14px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(245, 248, 255, 0.5); margin-bottom: 20px; color: rgb(10, 10, 10); border: 1.2px solid rgb(169, 172, 170);"><div class="zYSUYd" style="display: flex; flex-direction: row; overflow: hidden; border-radius: 8px 8px 0px 0px;"><div class="FS7GEb" style="display: flex; flex-direction: column; flex-grow: 1; min-width: 38%; width: 629.381px;"><div class="dDrxod" style="height: 34px; display: flex; justify-content: space-between; align-items: center; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(229, 237, 255); border-bottom: 1.2px solid rgb(169, 172, 170);"><div class="x7ndcb" style="padding-left: 16px; display: flex; align-items: center; color: rgb(10, 10, 10);">Code</div><div aria-live="polite" class="WDoJJe" style="margin-right: 4px; display: flex; align-items: center;"><button aria-label="Copy code to clipboard" class="hqI3tf B4zsNc" data-ved="2ahUKEwi2ko_Qnf2QAxUqRCoJHTDIOjAQ9MILegQIOBAB" role="button" style="margin: 0px 2px; background: none; height: 34px; border: none; color: rgb(5, 40, 97); position: relative; top: 2px; outline: 0px;" tabindex="0" title="Copy"><span class="z1asCe wm4nBd" style="display: inline-block; fill: currentcolor; height: 20px; line-height: 20px; position: relative; width: 20px;"><svg aria-hidden="true" enable-background="new 0 0 24 24" focusable="false" height="24" viewbox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><g><rect fill="none" height="24" width="24"></rect></g><g><path d="M16,20H5V6H3v14c0,1.1,0.9,2,2,2h11V20z M20,16V4c0-1.1-0.9-2-2-2H9C7.9,2,7,2.9,7,4v12c0,1.1,0.9,2,2,2h9 C19.1,18,20,17.1,20,16z M18,16H9V4h9V16z"></path></g></svg></span></button></div></div></div></div><div class="QQjpRc" style="display: flex; flex-direction: row; overflow: hidden; border-radius: 0px 0px 8px 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(245, 248, 255, 0.5);"><div class="FS7GEb" jsname="zEXxAd" style="display: flex; flex-direction: column; flex-grow: 1; min-width: 38%; width: 629.381px;"></div></div></div></div></div></div>```
// app/Contracts/MailerInterface.php
namespace App\Contracts;

interface MailerInterface
{
    public function sendMail(string $to, string $subject, string $body): string;
}

// app/Services/SmtpMailer.php
namespace App\Services;

use App\Contracts\MailerInterface;

class SmtpMailer implements MailerInterface
{
    public function sendMail(string $to, string $subject, string $body): string
    {
        return "Sending SMTP mail to {$to}: {$subject} - {$body}";
    }
}

// app/Providers/AppServiceProvider.php (or a custom Service Provider)
namespace App\Providers;

use App\Contracts\MailerInterface;
use App\Services\SmtpMailer;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->app->bind(MailerInterface::class, SmtpMailer::class);
    }
}
```

<div class="WaaZC" id="bkmrk-now%2C-anywhere-you-ty" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);"><div class="rPeykc" data-hveid="CDoQAQ" data-ved="2ahUKEwi2ko_Qnf2QAxUqRCoJHTDIOjAQo_EKegQIOhAB" style="margin: 10px 0px;"><span data-huuid="14580696178467078742">Now, anywhere you type-hint `MailerInterface`, the container will inject an instance of `SmtpMailer`.</span></div></div></div><div class="WaaZC" id="bkmrk-code-4" style="color: rgb(10, 10, 10); font-family: 'Google Sans', Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="RJPOee EIJn2" style="animation: auto ease 0s 1 normal none running none !important; color: rgb(10, 10, 10);"><div jsaction="rcuQ6b:npT2md" jscontroller="JegcYe"><div class="ecCNFc" style="border-image: none 100% / 1 / 0 stretch; border-radius: 8px; font-size: 14px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(245, 248, 255, 0.5); margin-bottom: 20px; color: rgb(10, 10, 10); border: 1.2px solid rgb(169, 172, 170);"><div class="zYSUYd" style="display: flex; flex-direction: row; overflow: hidden; border-radius: 8px 8px 0px 0px;"><div class="FS7GEb" style="display: flex; flex-direction: column; flex-grow: 1; min-width: 38%; width: 629.381px;"><div class="dDrxod" style="height: 34px; display: flex; justify-content: space-between; align-items: center; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(229, 237, 255); border-bottom: 1.2px solid rgb(169, 172, 170);"><div class="x7ndcb" style="padding-left: 16px; display: flex; align-items: center; color: rgb(10, 10, 10);">Code</div><div aria-live="polite" class="WDoJJe" style="margin-right: 4px; display: flex; align-items: center;"><button aria-label="Copy code to clipboard" class="hqI3tf B4zsNc" data-ved="2ahUKEwi2ko_Qnf2QAxUqRCoJHTDIOjAQ9MILegQIPRAB" role="button" style="margin: 0px 2px; background: none; height: 34px; border: none; color: rgb(5, 40, 97); position: relative; top: 2px; outline: 0px;" tabindex="0" title="Copy"><span class="z1asCe wm4nBd" style="display: inline-block; fill: currentcolor; height: 20px; line-height: 20px; position: relative; width: 20px;"><svg aria-hidden="true" enable-background="new 0 0 24 24" focusable="false" height="24" viewbox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><g><rect fill="none" height="24" width="24"></rect></g><g><path d="M16,20H5V6H3v14c0,1.1,0.9,2,2,2h11V20z M20,16V4c0-1.1-0.9-2-2-2H9C7.9,2,7,2.9,7,4v12c0,1.1,0.9,2,2,2h9 C19.1,18,20,17.1,20,16z M18,16H9V4h9V16z"></path></g></svg></span></button></div></div></div></div><div class="QQjpRc" style="display: flex; flex-direction: row; overflow: hidden; border-radius: 0px 0px 8px 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(245, 248, 255, 0.5);"><div class="FS7GEb" jsname="zEXxAd" style="display: flex; flex-direction: column; flex-grow: 1; min-width: 38%; width: 629.381px;"></div></div></div></div></div></div>```
// In a controller or job
use App\Contracts\MailerInterface;

class OrderController extends Controller
{
    public function __construct(protected MailerInterface $mailer)
    {
        // $this->mailer is an instance of SmtpMailer
    }

    public function processOrder()
    {
        $this->mailer->sendMail('user@example.com', 'Order Confirmation', 'Your order has been placed.');
    }
}
```