In this episode, we're diving into what a container is and how we use one in our PHP framework. We start by introducing the concept: think of a container as a kind of box where you can register (store) services or objects, and then grab them out whenever you need them elsewhere in your application. It's a powerful way to manage dependencies and organize your app.
First, we choose the league/container package, a commonly used container in PHP, and show how to install it with Composer. Once it's in, we do a quick check to make sure everything's working by creating a new container and dumping its contents.
Then, we see how to add an item (in the example, just a simple name string) to the container and how to get it back out. It's a basic demonstration, but it sets up the essential idea: instead of scattering your objects or services throughout your code, you use the container to keep things organized and accessible from anywhere.
We wrap up by thinking ahead: as your app grows, you’ll probably be adding more and more services (like CSRF protection, database access, and views). Doing all this setup code inline isn’t great, so next up, we tease out the idea of using service providers for a much neater, more scalable way to handle all your services. Stay tuned for that!