Namespaces and autoloading explained
In a project with third-party libraries there are bound to be several classes called “Client” or “Logger”. Namespaces solve this — and autoloading ensures that nobody has to include files by hand any more.
What you will learn
A namespace is a surname for classes
Without namespaces, each class could exist only once per application. Since every project uses third-party libraries today, that would be untenable. A namespace puts a path in front of the class name.
// File: src/Auftrag/Position.php
declare(strict_types=1);
namespace Firma\Shop\Auftrag;
final class Position
{
// ...
}
// The fully qualified name is now:
// Firma\Shop\Auftrag\PositionA second class `Position` in a different namespace is no problem, because the fully qualified name is different.
use shortens names, it loads nothing
declare(strict_types=1);
namespace Firma\Shop\Rechnung;
use Firma\Shop\Auftrag\Position;
use Firma\Lager\Position as Lagerposition;
final class Rechnungszeile
{
public function __construct(
private readonly Position $position,
private readonly ?Lagerposition $bestand = null,
) {
}
}A common misconception: `use` does not include a file. It only defines which fully qualified name is meant when the short one appears in the code. The file is loaded only on first access — by autoloading.
Autoloading: from name to file path
In the past, every file began with a series of `require` lines. Today you register a function that PHP calls as soon as an unknown class is needed. In practice, Composer handles this according to the PSR-4 convention: a namespace prefix is mapped to a directory, and the rest of the name to the path beneath it.
{
"autoload": {
"psr-4": {
"Firma\\Shop\\": "src/"
}
}
}The mapping is literal: upper and lower case matter, because the file system on the server distinguishes them — even if your own machine perhaps does not.
An example from practice
“Class not found”: the typical causes
Two levels of autoloader optimisation are often confused. `composer dump-autoload -o` turns the PSR-4 and PSR-0 rules into a class map, a ready-made list of class and file. For classes that are not in this list, for example because they were added after it was generated, the PSR-4 or PSR-0 rule still applies: the autoloader looks for them on the file system. Only `composer dump-autoload -a`, or the setting `classmap-authoritative`, makes the list authoritative. Anything missing from it is treated as non-existent, and the file system is no longer searched. A class added after generation then ends in “Class not found” until the class map is generated again. The Composer documentation recommends the first level for every production environment and the second only with care.
Paths that do not match the namespace exactly are a separate matter, for example the directory `src/auftrag/` for the namespace `Firma\Shop\Auftrag`. A Linux server distinguishes upper and lower case in the file system; the PSR-4 rule does not find the file there, with or without optimisation. On a machine whose file system does not distinguish case, as with the default settings of Windows and macOS, the same class can be loaded. That is why the error often only shows up on the server. When generating the class map with `-o` or `-a`, Composer reports such classes with “does not comply with psr-4 autoloading standard” and leaves them out. That message is a good checkpoint for every deployment.
Common misconceptions
Further sources
- PHP manual: Namespaces the language reference
- PSR-4: Autoloader the convention behind the mapping
- Composer: Autoloading the configuration in detail
- Composer: Autoloader optimization levels -o and -a compared
About the author
Fabian Lücker is managing partner of PHP Schmiede KG in Rheine and has worked in software development since 2007. He is responsible for development and design, and writes here about decisions that arise in real projects.