PHP Namespaces
Also Known As
PHP namespaces
namespace keyword
use statement
TL;DR
Logical groupings that prevent class name collisions and form the basis of PSR-4 autoloading.
Explanation
PHP namespaces (5.3+) encapsulate classes, interfaces, traits, functions, and constants into named scopes using namespace App\Controller. The fully qualified class name (FQCN) like App\Controller\HomeController maps directly to a file path under PSR-4. Use statements (use App\Service\UserService) alias FCQNs for cleaner code. The global namespace is accessed with a leading backslash (\Exception). Namespaces are essential for composer autoloading and preventing collisions between third-party packages.
Common Misconception
✗ Namespaces prevent class name conflicts automatically. Namespaces only prevent conflicts between classes in different namespaces — two classes with the same fully-qualified name in different files will still conflict. Namespaces organise code; Composer autoloading enforces uniqueness.
Why It Matters
Namespaces eliminate class name collisions between your code, third-party packages, and the PHP standard library. Without them, every package would need globally unique class names, making modern dependency management impossible.
Common Mistakes
- Mixing namespaced and non-namespaced code in the same project — it creates unpredictable autoloading behaviour.
- Forgetting that the global namespace requires a leading backslash inside a namespace (\Exception, not Exception).
- Using deeply nested namespaces (App\Modules\Orders\Domain\Entities\OrderLine) when shallower ones suffice.
- Not aliasing long namespace imports with use — leads to unreadable fully-qualified class names inline.
Code Examples
✗ Vulnerable
// No namespace — class name collisions in large projects:
class Logger {} // Conflicts with any other Logger class
class Request {} // Conflicts with framework's Request class
// With namespaces — no collision:
namespace App\Logging;
class Logger {} // App\Logging\Logger
namespace Vendor\Http;
class Request {} // Vendor\Http\Request — distinct
✓ Fixed
<?php
// File: src/Domain/Order/OrderService.php
declare(strict_types=1);
namespace App\Domain\Order;
use App\Domain\User\User; // import from another namespace
use App\Infrastructure\Mailer;
use DateTimeImmutable; // global namespace class
use InvalidArgumentException as InvalidArg; // alias
class OrderService {
public function place(User $user, array $items): Order {
if (empty($items)) {
throw new InvalidArg('Items cannot be empty');
}
return new Order($user, $items, new DateTimeImmutable());
}
}
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
15 Mar 2026
Edited
22 Mar 2026
Views
27
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
Amazonbot 1
Amazonbot 9
Perplexity 4
Google 4
Ahrefs 2
ChatGPT 2
SEMrush 2
How they use it
crawler 19
crawler_json 4
Related categories
⚡
DEV INTEL
Tools & Severity
🟢 Low
⚙ Fix effort: Low
⚡ Quick Fix
Follow PSR-4: namespace App\Services\Payment maps to src/Services/Payment.php — configure in composer.json autoload section
📦 Applies To
PHP 5.3+
web
cli
queue-worker
🔗 Prerequisites
🔍 Detection Hints
PHP files without namespace declaration or namespace not matching PSR-4 directory structure
Auto-detectable:
✓ Yes
phpcs
phpstan
composer
⚠ Related Problems
🤖 AI Agent
Confidence: Medium
False Positives: Medium
✓ Auto-fixable
Fix: Low
Context: File