Declaring and Using Namespaces
Wrap classes in namespaces and import them with use statements.
What is a Namespace?
A namespace groups related classes, functions, and constants to avoid naming collisions between libraries.
Declaring a Namespace
Use the namespace keyword as the first statement in a PHP file (after <?php).
<?php
namespace App\Services;
class Logger {
public function log(string $msg): void {
echo $msg;
}
}All lessons in this course
- Declaring and Using Namespaces
- Nested Namespaces and Aliases
- PSR-4 Autoloading Standard
- Registering Autoloaders