1. 46
    Replace PHP require statements with the Composer class autoloader
    4m 13s

Replace PHP require statements with the Composer class autoloader

Mark Shust
InstructorMark Shust
Share this video with your friends

Social Share Links

Send Tweet

Using "require" for classes isn't a modern practice for PHP development. A much better option is to use Composer's autoloader to automatically import classes into your app.

Instructor: [0:00] We are still manually importing files and classes all around our code with the require statements. This isn't a common practice in modern PHP development once you start working with classes.

[0:13] It would be very cumbersome to manage, and hard-coded file paths will make your app a bit tough to maintain. Instead, you can use what's called a PHP autoloader. It's common to use composers, dump autoload functionality to do this.

[0:29] That's exactly what we're going to do to set this all up. If you don't already, be sure to install composer. You can confirm it's installed on your machine by running composer--version. If a version number is outputted, it tells you that it is installed.

[0:47] First, we need to instantiate a new composer.json file, which is used for composer configuration for this project. If you don't already have one set up, you can run a new line called composer init. You'll have a lot of prompts come up.

[1:06] Let's just accept all of the defaults. At the time of this lesson, there are 13 prompts. Just hit yes or enter for each one of these. After this is executed, you will wind up with a new composer.json file in the root of your project.

[1:26] What we need to do is map the app namespace to the classes directory. This is very simple to do. The autoload is defined with something called PSR-4 format. All we need to do is reference the namespace within this property name. This will just be app/.

[1:46] When you have slashes within this config, you need to make sure to escape it. A single slash becomes a double slash, just as its represented here. Finally, the value of this namespace is mapped to the location that it can be found within our file system.

[2:05] This will be the classes subdirectory. By default, composer looks at the source directory. We're not using this so we can just remove it. After this is done, let's save the file. We need composer to register the classes with the autoloader. This is very easy with command line.

[2:26] All we need to do is run composer dump-autoload. This will generate all of the includes that we need for our namespace to be auto imported. Now that this is all set up, we have one last step and that is to remove all of these require statements.

[2:48] We can do this from all of our files, which at the moment are just the post and index.php files. We only need one require now, and that will be the require from vendor/autoload.php.

[3:06] This kicks off the autoload process, which automatically imports all of the files with these other PHP files that are defined in the vendor/composer directory. All we need to do is include that file with a require statement in the first file of our app, which is index.php.

[3:27] Let's type require, and the file we are requiring is at vendor/autoload.php. This is the only file we need to require in our app. The autoloader will automatically require all of the classes that we call in our code with include or require statements.

[3:48] When we save and refresh our page, everything still works with one require statement. All of the classes that we are referencing in our app are automatically included by the autoloader.

[4:02] You may need to periodically run the composer dump autoload command when new route namespaces are being added to your code base.