How to autoload the PSR-4 way?
PSR-4 is the newest standard of autoloading in PHP, and it compels us to use namespaces.
We need to take the following steps in order to autoload our classes with PSR-4:
a. Put the classes that we want to autoload in a dedicated directory. For example, it is customary to convene the classes that we write into a directory called src/,
thus, creating the following folder structure:
your-website/
src/
Db.php
Page.php
User.php
b. Give the classes a namespace. We must give all the classes in the src/ directory the same namespace.
For example, let's give the namespce Acme to the classes. This is what the Page class is going to look like:
<?php
namespace Acme;
class Page {
public function __construct()
{
echo "hello, i am a page.";
}
}
- We give the same namespace Acme to all of the classes in the src/ directory.
c. Point the namespace to the src/ directory in the composer.json file .
We point the directory that holds the classes to the namespace in the composer.json file.
For example, this is how we specify in the composer.json file that we gave the namespace Acme to the classes in the src/ directory:
{
"autoload": {
"psr-4": {
"Acme\\":"src/"
}
}
}
- We use the psr-4 key.
- The namespace Acme points to the src/ directory.
- The namespace has to end with \\. For example, "Acme\\".
- You can replace the generic Acme with the name of your brand or website.
d. Update the Composer autoloader:
$ composer dumpautoload -o
e. Import the namespace to your scripts. The scripts need to import the namespace as well as the autoloader, e.g., index.php:
<?php
require "vendor/autoload.php";
use Acme\Db;
use Acme\User;
use Acme\Page;
$page1 = new Page();