Declaring and Using Namespaces
Wrap classes in namespaces and import them with use statements.
Declaring and Using Namespaces is a free PHP Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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;
}
}Namespace Segments
Segments are separated by backslashes (\). Each segment typically maps to a directory in PSR-4 autoloading.
Importing with use
The use keyword imports a fully-qualified class name so you reference it by its short name.
<?php
namespace App\Controllers;
use App\Services\Logger;
class HomeController {
public function index(): void {
$logger = new Logger();
$logger->log("visited");
}
}Fully-Qualified Names
Reference a class without use by prefixing with a leading backslash.
<?php
$logger = new \App\Services\Logger();
$logger->log("Hello");The Global Namespace
Code without a namespace lives in the global namespace. Built-in classes like \DateTime also live there. Inside a namespace, prefix with \.
Importing Functions and Constants
Use use function and use const to import non-class symbols.
<?php
namespace App;
use function App\Utils\formatDate;
use const App\Utils\DATE_FORMAT;
echo formatDate(time(), DATE_FORMAT);Grouped use Statements
Import multiple symbols in a single grouped use block (PHP 7+).
<?php
use App\Models\{User, Post, Comment};One Namespace Per File
Multiple namespace declarations in one file are allowed but discouraged. Prefer one namespace per file for clarity and autoloader compatibility.
Common Conventions
Laravel and Symfony use App\ as the root namespace mapped to src/ or app/ in composer.json.
Sub-namespace Organisation
Group by feature or layer: App\Http\Controllers, App\Domain\Orders, App\Infrastructure\Database.
Namespace Recap
Key points: namespace must be first statement; use imports symbols; backslash separates segments; prefix \ for global classes inside a namespace.
Quick Check
Which keyword imports a class from another namespace?
Frequently asked questions
Is the “Declaring and Using Namespaces” lesson free?
Yes — the full text of “Declaring and Using Namespaces” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Declaring and Using Namespaces”?
Wrap classes in namespaces and import them with use statements. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Declaring and Using Namespaces” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Declaring and Using Namespaces
- Nested Namespaces and Aliases
- PSR-4 Autoloading Standard
- Registering Autoloaders