Content · Web Development · PHP

PHP ReflectionClass::getConstant() Method: Usage and Examples

PHP ReflectionClass::getConstant() Method: Usage and Examples: Returns the value of a named class constant, or false when no such constant exists.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

ReflectionClass inspects the definition of a class, rather than running its constructor or calling its methods.

What Is ReflectionClass::getConstant in PHP?


Returns the value of a named class constant, or false when no such constant exists.

ReflectionClass::getConstant(string $name)

Parameters:

  • $name — Required. The exact class-member name (method, property or constant) to inspect.

Example 1: Understanding the Basic Behavior


<?php

class Report { public const VERSION = 2; public string $title = 'Draft'; protected int $pages = 10; public static string $category = 'docs'; public function publish(): void {} }
$reflectionClass = new ReflectionClass(Report::class);
$result = $reflectionClass->getConstant("VERSION");
var_dump($result);

Output:

int(2)

Explanation: Returns the value of a named class constant, or false when no such constant exists.

Example 2: Testing Another Case


<?php

interface Exportable { public function export(): string; }
trait HasId { public function getId(): int { return 12; } }
class Invoice implements Exportable { use HasId; public string $name = 'Invoice'; public const STATE = 'open'; public function export(): string { return 'invoice'; } }
$reflectionClass = new ReflectionClass(Invoice::class);
$result = $reflectionClass->getConstant("STATE");
var_dump($result);

Output:

string(4) "open"

Explanation: A missing constant returns false; a constant can itself have the value false.

Practical Example: Applying the Operation


<?php

abstract class BaseJob { protected string $name = 'job'; abstract public function run(): void; }
class CompletedJob extends BaseJob { public function run(): void {} }
$reflectionClass = new ReflectionClass(CompletedJob::class);
$result = $reflectionClass->getConstant("MISSING");
var_dump($result);

Output:

bool(false)

Explanation: The returned metadata may be false, null, an array, or a Reflection object depending on the requested property.

Important Behavior and Edge Cases


  • A missing constant returns false; a constant can itself have the value false.
  • The returned metadata may be false, null, an array, or a Reflection object depending on the requested property.

Comparison With Related Approaches


ApproachWhen to Use It
ReflectionClass::getConstantReturns the value of a named class constant, or false when no such constant exists.
ReflectionClass::getReflectionConstant()getConstant() returns the value; getReflectionConstant() returns metadata about the declared constant.

Common Beginner Mistakes


MistakeCorrect Approach
Treating metadata returned by ReflectionClass::getConstant() as though it executes the reflected class member.A missing constant returns false; a constant can itself have the value false.
Choosing ReflectionClass::getReflectionConstant() without checking whether it provides the same result.getConstant() returns the value; getReflectionConstant() returns metadata about the declared constant.

Key Takeaways


  • Returns the value of a named class constant, or false when no such constant exists.
  • A missing constant returns false; a constant can itself have the value false.
  • The returned metadata may be false, null, an array, or a Reflection object depending on the requested property.

The examples demonstrate where ReflectionClass::getConstant fits within reflection and metadata. Verify the relevant preconditions whenever you adapt the code to real data.

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