In this chapter
We'll learn exactly how PHP turns a name in your code into a full name — fully qualified, qualified and unqualified names, why a class name inside a namespace never falls back to the global namespace (so new DateTime() failed) while functions and constants do (so strlen() worked), the namespace keyword, ::class, and why names in strings are always fully qualified.
The Problem in Real Life
Mia moves Booking into its namespace: namespace BookBay\Booking; at the top of Booking.php. Its isPast() method creates new DateTime($this->start) — and now fails with "Class "BookBay\Booking\DateTime" not found".
A few lines further down, strlen($this->note) and sprintf("%02d", ...) work exactly as before. Both are PHP's own, both live in the global namespace, and neither was imported.
DateTime and strlen are both PHP's. Why does PHP find one and not the other?
Mia
Classes vs. Functions: Who Falls Back to Global?
Three kinds of names
\BookBay\Booking, Booking\Status and Booking look alike but are resolved differently.
Different rules
Unqualified class names stay in the current namespace. Function and constant names fall back to the global one.
Names in strings
A class name stored in a string is always read as a full name — imports don't apply.
How PHP Resolves Names
From the name you write to the full name: every time PHP meets a class, function or constant name, it works out the fully qualified name. This chapter gives the exact rules:
- Three forms of name: fully qualified, qualified and unqualified.
- Classes: imports, then the current namespace — no fallback.
- Functions and constants: the current namespace, then the global one.
- namespace\, ::class and strings: the special cases.
Three forms of name: a fully qualified name starts with a backslash, \BookBay\Booking\Booking: it's absolute, like a path starting with /, and needs no resolving. A qualified name has a backslash inside but not at the start, Booking\BookingStatus: it's relative. An unqualified name has no backslash at all, Booking: it's resolved from the file's imports and current namespace. The table below lists the cases.
| You write | Form | Class becomes | Function or constant becomes |
|---|---|---|---|
| \DateTime / \strlen() | Fully qualified | DateTime | strlen |
| Money | Unqualified, imported | BookBay\Pricing\Money | — |
| BookingStatus | Unqualified | BookBay\Booking\BookingStatus | BookBay\Booking\… then global |
| DateTime | Unqualified | BookBay\Booking\DateTime (error) | — |
| strlen() | Unqualified | — | BookBay\Booking\strlen, else \strlen |
| Status\Label | Qualified | BookBay\Booking\Status\Label | BookBay\Booking\Status\… |
| namespace\Money | Explicitly current | BookBay\Booking\Money | BookBay\Booking\… |
| "BookBay\\Pricing\\Money" (string) | Always fully qualified | BookBay\Pricing\Money | same |
Classes: imports, then the current namespace, nothing else: for an unqualified class name, PHP checks the file's use imports first; if there's no import, it prepends the current namespace, and that's the final answer. There is no fallback to the global namespace. Inside namespace BookBay\Booking;, new DateTime() means BookBay\Booking\DateTime — Mia's error. The fix is \DateTime with a leading backslash, or use DateTime; at the top, which many projects do for every built-in class they use. For a qualified class name, the first part is checked against imports, and otherwise the current namespace is prepended to the whole name.
<?phpnamespace BookBay\Booking;try {$start = new DateTime("2026-11-06 09:30"); // means BookBay\Booking\DateTime} catch (\Error $e) {echo $e->getMessage(), "\n"; // Class "BookBay\Booking\DateTime" not found}$start = new \DateTime("2026-11-06 09:30"); // fix 1: fully qualifiedecho $start->format("D H:i"), "\n"; // Fri 09:30
<?phpnamespace BookBay\Booking;use DateTime; // imports \DateTime under its short name$start = new DateTime("2026-11-06 09:30");echo $start->format("l"), "\n"; // Friday
Functions and constants: a fallback to global: unqualified function and constant names follow a different rule, because PHP's thousands of built-in functions and constants all live in the global namespace and nobody wants to write \strlen() everywhere. PHP looks for BookBay\Booking\strlen first, and if no such function exists, it falls back to the global \strlen. That's why strlen() and sprintf() kept working. The consequence: a namespace can define its own strlen() and override the global one for that namespace — rarely a good idea. (Writing \strlen() is also very slightly faster, since PHP can skip the first check; some tools add the backslash automatically.) The diagram below shows both rules side by side.
Inside namespace BookBay\Booking: new DateTime() vs. strlen()
new DateTime()
an unqualified CLASS name
strlen($note)
an unqualified FUNCTION name
1. A use import for DateTime?
none in this file
1. BookBay\Booking\strlen() exists?
no
2. Prepend the current namespace
→ BookBay\Booking\DateTime — final, no fallback
2. Fall back to the global namespace
→ \strlen() — PHP's own
Error: Class "BookBay\Booking\DateTime" not found
fix: \DateTime or use DateTime;
Works
global functions and constants are found automatically
<?phpnamespace BookBay\Reports {echo strlen("abc"), "\n"; // 3: no BookBay\Reports\strlen, so \strlen}namespace BookBay\Booking {function strlen(string $s): int // a namespace function with the same name{return 99;}echo strlen("abc"), " ", \strlen("abc"), "\n"; // 99 3: the namespace's own wins}
The namespace keyword as a prefix: namespace\Money means "Money in the current namespace", explicitly — the namespace equivalent of ./ in a path. You'll rarely see it; it exists for code that must be unambiguous even if an import of the same name is added later.
::class and names in strings: Booking::class gives the fully qualified name as a string, resolved at compile time with all the rules above: BookBay\Booking\Booking. Use it whenever a class name is needed as a string, like getAttributes(MaxLength::class) in Act 12. Names that are already strings are different: new $className, class_exists("BookBay\\Booking\\Booking") and callbacks like "BookBay\\Pricing\\formatPrice" are always treated as fully qualified — imports and the current namespace don't apply, and a leading backslash is optional. Note the double backslashes inside double-quoted strings (Act 07). Prefer ::class to typing names in strings: a typo there becomes an error the editor can see.
<?phpnamespace BookBay\Booking;const MAX_PER_DAY = 16;class Booking {}echo Booking::class, "\n"; // BookBay\Booking\Booking: resolved by PHPecho namespace\MAX_PER_DAY, "\n"; // 16: explicitly the current namespace$className = "BookBay\\Booking\\Booking"; // a string: always fully qualifiedecho get_class(new $className()), "\n"; // BookBay\Booking\Booking$wrong = "Booking"; // imports and namespace do NOT apply to stringsvar_dump(class_exists($wrong)); // bool(false)
Key Takeaway
Fully qualified names (\BookBay\Booking\Booking) are absolute; qualified (Booking\Status) and unqualified (Booking) names are resolved. Unqualified class names use the file's imports, else the current namespace — never the global namespace, so new DateTime() inside a namespace needs \DateTime or use DateTime. Unqualified function and constant names try the current namespace, then fall back to the global one, so strlen() keeps working. ::class gives the resolved full name as a string; names already in strings are always taken as fully qualified.
Why This Matters
"Class not found" right after adding a namespace is the most common namespace error in PHP, and it always comes down to these rules. Knowing that classes never fall back to global — but functions do — turns the error into a one-second fix.
The rules also explain framework code you'll read: why Laravel files start with use Illuminate\… lines, why ::class appears in every configuration file, and why a class name in a string must be written in full.
Booking imports DateTime, and every class in src/ has a namespace. But TextFlow's library still sits in lib/textflow/, copied from a zip. On Friday afternoon TextFlow announces version 2.4 with a security fix. Mia unzips it over the old folder — and the reminder script dies with "Class "GuzzleHttp\Client" not found", a library that 2.4 needs and the zip didn't include. And Marcus's own small fix inside lib/textflow/ is gone, overwritten. That's the next chapter: Composer.
