A namespace prefixes every class, interface, trait, enum, function and constant declared in it, so your Money and a library's Money can coexist. namespace must be the file's first statement (only declare() may precede it), and names run vendor first.
<?php
namespace Acme\ShopMoney; // the first statement (only declare() may precede)
const DEFAULT_LOCALE = 'en_US'; // defines \Acme\ShopMoney\DEFAULT_LOCALE
function cents(string $amount): int { return (int) round((float) $amount * 100); }
final class Money { public function __construct(public int $minor) {} }
echo __NAMESPACE__, ' | ', Money::class, ' | ', namespace\cents('12.50'), "\n";
echo DEFAULT_LOCALE, ' ', strlen('abc'), ' ', \strlen('abcd'), ' ', PHP_VERSION, "\n";
try {
$now = new DateTimeImmutable(); // classes never fall back to the global space
} catch (\Error $e) {
echo $e->getMessage(), "\n";
}Output
Acme\ShopMoney | Acme\ShopMoney\Money | 1250 en_US 3 4 8.5.4 Class "Acme\ShopMoney\DateTimeImmutable" not found
Names resolve like file paths: Money and Exception\CurrencyMismatch are relative to the current namespace, \DateTimeImmutable is absolute, and names in strings (new $class) are always absolute, without the leading backslash. A missing function or constant falls back to the global one (strlen, PHP_VERSION), but a class never does: write \DateTimeImmutable or import it (Subsection 4.12.3).