ReflectionConstant::inNamespace() is a new PHP core method that returns whether a reflected constant belongs to a namespace. It closes a long-standing gap: classes and functions already had this helper, constants did not.
TL;DR: my PR #20902 was merged into php-src, adding ReflectionConstant::inNamespace(): bool so you no longer have to split constant names on \ by hand.

Merged into PHP core (php-src): PR #20902 — Added ReflectionConstant::inNamespace() to align the Reflection API.
This change improves consistency across the Reflection API — the kind of polish that makes day-to-day developer experience better over time.
What was missing?
PHP’s Reflection API already offers an inNamespace() helper in other places (for example, on reflected classes and functions). But constants didn’t have an equivalent method.
That meant if you wanted to check whether a constant belongs to a namespace, you had to do something more manual, such as parsing the constant’s name or splitting by \\.
In practice that looked like this — correct enough, but duplicated across every codebase that needed it:
// before: string surgery on the constant name
$ref = new ReflectionConstant('My\\Lib\\MY_CONST');
$isNamespaced = str_contains($ref->getName(), '\\');
Hand-rolled checks like this are easy to get subtly wrong, and they encode an assumption about name formatting that belongs in the engine rather than in userland.
This PR fills that gap by introducing:
ReflectionConstant::inNamespace(): bool
Why it matters
Reflection is widely used in:
- Framework internals and service containers
- Auto-discovery and plugin systems
- Static analysis tooling
- Code generators
- Debugging and developer tooling
Having consistent helper methods across Reflection types makes those tools simpler, more readable, and less error-prone.
The deeper argument is about API symmetry. When code walks a mix of reflected classes, functions, and constants, every asymmetry becomes a special case at the call site. A generic routine that wants to group symbols by namespace has to branch purely because one Reflection type is missing a method the others have. Removing that branch is a small win repeated everywhere the API is used.
Example usage
$ref = new ReflectionConstant('My\\Lib\\MY_CONST');
if ($ref->inNamespace()) {
// Constant is namespaced
}
It pairs naturally with the accessors that were already there:
$ref->getName(); // 'My\Lib\MY_CONST'
$ref->getNamespaceName(); // 'My\Lib'
$ref->getShortName(); // 'MY_CONST'
$ref->inNamespace(); // true
This brings ReflectionConstant closer to the rest of the Reflection API, improving parity and predictability.
Notes for contributors
This change also includes documentation/notes updates (NEWS/UPGRADING) so the addition is visible to PHP developers following new core changes.
That step is worth highlighting for anyone considering their first php-src contribution. Updating NEWS and UPGRADING is not paperwork tacked on at the end — a change that is not discoverable by the people upgrading is, from their perspective, not shipped. Reviewers will ask for it, so including it in the initial PR saves a round trip.
A small, self-contained addition like this is also a realistic entry point. It required no RFC, because it adds a method consistent with existing precedent rather than changing language semantics — the bar that separates a straightforward PR from a proposal needing a vote.
Frequently asked questions
Which PHP version includes this?
It landed in php-src after the PR was merged, so it ships in the next feature release cut from that branch. Check the UPGRADING notes for the exact version.
Did this need an RFC?
No. RFCs are for changes to language semantics or contentious additions. Aligning one Reflection class with helpers the others already expose is treated as a consistency fix.
What does it return for a global constant?
false — a constant declared outside any namespace is not namespaced, matching how inNamespace() already behaves for classes and functions.
Link
If you want to review the diff or discussion, here’s the PR:
https://github.com/php/php-src/pull/20902
For the longer story of how I got started with php-src, see Contributing to PHP Core: Fixing a NAN Reflection Bug.
Comments