CodeBlanks Logo Icon CodeBlanks

Summary - Namespaces

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.

Practice 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__.

Starter code

index.php
<?php

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

namespace App\Repository {
    // Import the AccountService class with use

    // Add the UserRepository class and the find method

    // Call find, print owner() and __NAMESPACE__
}

Sample solution

index.php
<?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";
}

Expected output

Alex
App\Repository

Practice 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.

Starter code

index.php
<?php

// Define the classToFile function (App\ → src/)

// Register it with spl_autoload_register

// Print paths for App\Models\User and App\Services\AccountService

Sample solution

index.php
<?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";

Expected output

src/Models/User.php
src/Services/AccountService.php

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which keyword imports a class?

  • import
  • use Correct answer
  • include
  • require

Explanation: The use keyword imports a class.

All lessons in Advanced