0PricingLogin
PHP Academy · Lesson

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

  1. Declaring and Using Namespaces
  2. Nested Namespaces and Aliases
  3. PSR-4 Autoloading Standard
  4. Registering Autoloaders
← Back to PHP Academy