Content · Programming · PHP

PHP ReflectionConstant::getName() Method: Usage and Examples

Returns the full declared name of the reflected global constant.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 Know-Stuff

When building tools that inspect PHP code, ReflectionConstant::getName() helps answer a focused question: returns the full declared name of the reflected global constant.

Reflection is useful in framework tooling, dependency injection, testing, code inspection and debugging.

What Is ReflectionConstant::getName()?


Returns the full declared name of the reflected global constant.

Its method signature is:

public function getName(): string

This method has no required arguments.

Return value: string

Example 1: Inspect the basic behavior


Start with a self-contained PHP declaration and inspect the method result.

<?php

declare(strict_types=1);

const GUIDE_LEVEL = 7;
const GUIDE_LIMIT = 12;
$constantReflection = new ReflectionConstant("GUIDE_LEVEL");

$result = $constantReflection->getName();
var_dump($result);

Output:

string(11) "GUIDE_LEVEL"

Explanation: Returns the full declared name of the reflected global constant.

Example 2: Compare a second case


Next, change the reflected declaration or parameter to observe a related situation.

<?php

declare(strict_types=1);

const GUIDE_LEVEL = 7;
const GUIDE_LIMIT = 12;
$constantReflection = new ReflectionConstant("PHP_VERSION_ID");

$result = $constantReflection->getName();
var_dump($result);

// Check the returned value type before using the result.
echo "\nResult type: " . get_debug_type($result);

Output:

string(14) "PHP_VERSION_ID"

Result type: string

Explanation: Namespaced constant names can include backslashes.

Practical Example: Apply the operation in context


This final case shows how the same API can be used when inspecting application definitions.

<?php

declare(strict_types=1);

const GUIDE_LEVEL = 7;
const GUIDE_LIMIT = 12;
$constantReflection = new ReflectionConstant("GUIDE_LIMIT");

$result = $constantReflection->getName();
var_dump($result);

// Use the reflection result in a simple decision.
if (is_bool($result)) {
    echo "\nStatus: " . ($result ? "yes" : "no");
} elseif (is_array($result)) {
    echo "\nEntries: " . count($result);
} elseif ($result === null) {
    echo "\nNo value returned";
} else {
    echo "\nReflection result available";
}

Output:

string(11) "GUIDE_LIMIT"

Reflection result available

Explanation: getShortName removes the namespace portion.

Important Behavior and Edge Cases


  • Namespaced constant names can include backslashes.
  • getShortName removes the namespace portion.
  • Reflection describes declarations and runtime state; requesting metadata does not automatically validate or sanitize application input.

Comparison With Related Approaches


ApproachWhen to Use It
ReflectionConstant::getName()Returns the full declared name of the reflected global constant.
Related APIgetShortName removes the namespace portion.

Common Beginner Mistakes


MistakeCorrect Approach
Ignoring this condition: Namespaced constant names can include backslashes.Handle the specific limitation: Namespaced constant names can include backslashes.
Confusing this method with Related APIgetShortName removes the namespace portion.

Key Takeaways


  • Returns the full declared name of the reflected global constant.
  • Namespaced constant names can include backslashes.
  • getShortName removes the namespace portion.

Use ReflectionConstant::getName() when you need the precise metadata or behavior demonstrated above. Keep the target declaration and relevant PHP version in mind when adapting the examples.

Share this resourceSend this page to someone who may find it useful.