Namespaces and Autoloading
Organizing classes with namespaces, and the PSR-4 convention that autoloads them.
What you'll learn
- Declare a namespace and reference a class in another namespace by its fully-qualified name
- Use a `use` statement to import a class under its short name
- Explain, conceptually, what PSR-4 autoloading maps a namespace to
Explanation
A namespace groups related classes under a shared prefix, avoiding name collisions between, say, your own Calculator class and a third-party library's class of the same name: namespace App; class Calculator { ... } puts Calculator under the App namespace.
From outside that namespace, you can reference the class by its fully-qualified name, starting with a leading backslash: \App\Calculator::add(3, 4). Typing that out repeatedly is tedious, so a use statement imports the class under its short name for the rest of the file: use App\Calculator; lets you write just Calculator::add(3, 4) afterward.
In a real, multi-file PHP project, you don't manually require every class file you need -- PSR-4 autoloading (a widely-adopted PHP community convention, almost always set up via Composer, covered next lesson) maps a namespace prefix to a directory on disk, so referencing App\Calculator automatically loads the file that defines it, without an explicit require anywhere in your code.
This walkthrough shows two files (marked by comments) as PHP would see them once loaded -- in a real project, autoloading (or an explicit require) is what gets the second file's code access to the first file's class in the first place.
Guided lab
Guided edit: From a fully-qualified name to a use statement
Follow each step to see two ways of referencing a class in another namespace.
Step 1 of 2
Reference the class by its fully-qualified name, starting with a leading backslash -- no `use` statement needed.
<?php
// file: src/Calculator.php
namespace App;
class Calculator {
public static function add(int $a, int $b): int {
return $a + $b;
}
}
// file: index.php
echo \App\Calculator::add(3, 4) . "\n";Stuck? Get a hint.
Common mistakes
- Forgetting the leading backslash when writing a fully-qualified class name from outside its namespace, e.g. `App\Calculator` instead of `\App\Calculator`.
- Assuming a `use` statement executes code or loads a file -- it only creates a short-name alias for something that must already be loadable (typically via autoloading).
- Thinking PSR-4 autoloading is a PHP language feature -- it's a community convention, implemented by tooling like Composer's generated autoloader, not by the PHP engine itself.
Knowledge check
Takeaway
A fully-qualified name (`\App\Calculator`) always works; `use` just creates a shorter alias for it, and PSR-4 autoloading (via tooling like Composer) is what makes referencing a class actually load its file.
Summary
Namespaces group classes to avoid name collisions; `use` imports a short alias; PSR-4 autoloading conventionally maps a namespace prefix to a directory so classes load without manual `require` calls.
References
Your notes
Notes save automatically.
Finished this lesson?
Mark it complete to track your progress and schedule a future review.
AI tutor
The optional AI tutor isn't enabled in this deployment. All lessons, exercises, quizzes, and search work fully without it.