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
Published 2 years ago
Updated 2 years ago

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.

~ 2 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today