PR #20902 Merged into PHP Core: ReflectionConstant::inNamespace()

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

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.


Example usage

$ref = new ReflectionConstant('My\\Lib\\MY_CONST');

if ($ref->inNamespace()) {
    // Constant is namespaced
}


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.

Link

If you want to review the diff or discussion, here’s the PR:

https://github.com/php/php-src/pull/20902

Leave a Reply

Your email address will not be published. Required fields are marked *