Nested Namespaces and Aliases
Create sub-namespaces and use aliases to avoid verbosity.
Nested Namespaces and Aliases is a free PHP Academy lesson on CoddyKit — lesson 2 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.
Nested Namespace Example
PHP namespaces can be deeply nested with more backslash-separated segments.
<?php
namespace Vendor\Package\Http\Middleware;
class AuthMiddleware {
public function handle(): void {}
}Why Aliases?
Long namespace paths make code verbose. Aliases let you refer to a class by a shorter name you choose.
Aliasing a Class
Use use … as Alias to rename an import for the current file.
<?php
namespace App\Controllers;
use App\Infrastructure\Database\MySQLConnection as DB;
class UserController {
public function __construct(private DB $db) {}
}Aliasing a Namespace Prefix
You can alias a namespace prefix to shorten deeply nested references.
<?php
use App\Domain\Orders\Repositories as OrderRepo;
$repo = new OrderRepo\EloquentOrderRepository();Resolving Class Name Conflicts
When two packages define a class with the same short name, alias one of them.
<?php
use League\Flysystem\Filesystem;
use Illuminate\Filesystem\Filesystem as LaravelFS;
$fs1 = new Filesystem(/* adapter */);
$fs2 = new LaravelFS();Grouped use with Aliases
Grouped use blocks also support aliases per symbol.
<?php
use App\Models\{User, Post as Article};Deeply Nested Structures
Real-world DDD projects often have 4+ segment namespaces, e.g. App\Domain\Billing\Services\InvoiceGenerator.
Alias Scope
Aliases are file-scoped. An alias defined in one file has no effect in another.
Two Logger Libraries
Example: using both Monolog and a custom logger.
<?php
use Monolog\Logger;
use App\Logging\Logger as AppLogger;
$monolog = new Logger("channel");
$app = new AppLogger();Function and Constant Aliases
You can also alias imported functions and constants.
<?php
use function App\Helpers\formatMoney as money;
echo money(1999); // "€19.99"Best Practices
Keep aliases meaningful. Use aliases to resolve conflicts, not to save typing at the expense of readability.
Summary
Nested namespaces model your directory structure. Aliases resolve name conflicts and reduce verbosity — both use use … as at the top of the file.
Quick Check
How do you alias a class import?
Frequently asked questions
Is the “Nested Namespaces and Aliases” lesson free?
Yes — the full text of “Nested Namespaces and Aliases” 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 “Nested Namespaces and Aliases”?
Create sub-namespaces and use aliases to avoid verbosity. 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Nested Namespaces and Aliases” 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