CodeBlanks Logo Icon CodeBlanks

Summary - Namespaces

PHP course · Advanced · Lesson 33/92

Build a modular library structure with namespace, use, alias and PSR-4.

Summary - Namespaces

In this group you learned to package code with namespace, import with use, alias with as, the global namespace, and multi-level structures. Finally you wrote a PSR-4 autoloader.

Now you will combine them to build a modular library skeleton: define classes in their own namespaces and load them automatically with an autoloader.

Modular library skeleton

In a PSR-4 compliant library every class lives in its own file. The App\Services\AccountService class is defined in src/Services/AccountService.php; the caller imports it with use App\Services\AccountService;.

The autoloader loads the right file automatically. So no manual require is needed; as the project grows this matters.

Exercise Use a class across multi-level namespaces

Define two namespaces with braced syntax. Inside App\Services add an AccountService class with an owner() method (returning "Alex"). Inside App\Repository, import App\Services\AccountService with use; the UserRepository class's static find(int $id): AccountService method should return new AccountService(). Get the instance with $user = UserRepository::find(42), then print the owner() output and __NAMESPACE__.

Show example solution
AccountService { return new AccountService(); } }. Call $user = UserRepository::find(42); echo $user->owner() . "\n"; echo __NAMESPACE__ . "\n";.
Example solution:
<?php

namespace App\Services {
    class AccountService {
        public function owner(): string {
            return "Alex";
        }
    }
}

namespace App\Repository {
    use App\Services\AccountService;

    class UserRepository {
        public static function find(int $id): AccountService {
            return new AccountService();
        }
    }

    $user = UserRepository::find(42);
    echo $user->owner() . "\n";
    echo __NAMESPACE__ . "\n";
}

Exercise PSR-4 autoloader with multiple class paths

Write a classToFile(string $class): string function: strip the App\ prefix, turn the remaining \ into /, prepend src/ and append .php. Register it with spl_autoload_register. Print the file paths for both App\Models\User and App\Services\AccountService.

Show example solution
string { $prefix = "App\\"; $base = "src/"; $relative = str_replace($prefix, "", $class); $relative = str_replace("\\", "/", $relative); return $base . $relative . ".php"; }. Register with spl_autoload_register. Print with echo classToFile("App\\Models\\User") . "\n"; and echo classToFile("App\\Services\\AccountService") . "\n";.
Example solution:
<?php

// Define the classToFile function (App\ → src/)
function classToFile(string $class): string {
    $prefix = "App\\";
    $base = "src/";
    $relative = str_replace($prefix, "", $class);
    $relative = str_replace("\\", "/", $relative);
    return $base . $relative . ".php";
}

// Register it with spl_autoload_register
spl_autoload_register(function ($class) {
    $file = classToFile($class);
    require $file;
});

// Print paths for App\Models\User and App\Services\AccountService
echo classToFile("App\\Models\\User") . "\n";
echo classToFile("App\\Services\\AccountService") . "\n";

Test yourself

  1. Which keyword imports a class?

    • use
    • import
    • include
    • require
    Show answer

    use — Correct. The use keyword imports a class.

All lessons in Advanced