In this episode, we get a quick and practical introduction to setting up PSR-4 autoloading in a PHP project using Composer. We start with an empty folder and the terminal, and it doesn't matter what operating system you're on—the steps are the same for everyone.
First, we cover how to download Composer, either globally or just for your project. The video shows how to grab Composer using PHP if you don't have curl available. Once Composer is set up (as composer.phar), we jump into creating a composer.json
file, which is where we tell Composer about our PSR-4 autoloading setup. You'll see how to specify your application's namespace and the source directory (like app
or src
).
After configuring autoloading, we generate the autoload files using Composer's dump-autoload
command. Next, we set up an app
directory and create some PHP classes inside it, making sure to use namespaces correctly. You'll learn how your directory structure maps to your namespaces under the PSR-4 standard.
To test things out, we create a simple bootstrap file to require Composer's autoload script, and then spin up a sample class to confirm autoloading is working. We also touch on best practices for deploying your code—like only uploading your composer.json
and then running Composer on your server, instead of copying the entire vendor
folder.
By the end, you'll see how Composer makes dependency management and autoloading a breeze in modern PHP projects, all done in just a few quick steps.