Content · Web Development · PHP

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

PHP ReflectionClass::getConstants() Method: Usage and Examples: Returns a name-to-value array of class constants visible through reflection.

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::getConstants in PHP?


Returns a name-to-value array of class constants visible through reflection.

ReflectionClass::getConstants(?int $filter = ...)

Parameters:

  • $filter — Optional. An optional bitmask of ReflectionMethod or ReflectionProperty modifiers, depending on the method.

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->getConstants();
var_dump($result);

Output:

array(1) {
  ["VERSION"]=>
  int(2)
}

Explanation: Returns a name-to-value array of class constants visible through reflection.

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->getConstants();
var_dump($result);

Output:

array(1) {
  ["STATE"]=>
  string(4) "open"
}

Explanation: This method returns names mapped directly to constant values; descriptors require getReflectionConstants().

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->getConstants();
var_dump($result);

Output:

array(0) {
}

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

Important Behavior and Edge Cases


  • This method returns names mapped directly to constant values; descriptors require getReflectionConstants().
  • 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::getConstantsReturns a name-to-value array of class constants visible through reflection.
ReflectionClass::getReflectionConstants()getConstants() returns name/value pairs; getReflectionConstants() returns descriptor objects.

Common Beginner Mistakes


MistakeCorrect Approach
Treating metadata returned by ReflectionClass::getConstants() as though it executes the reflected class member.This method returns names mapped directly to constant values; descriptors require getReflectionConstants().
Choosing ReflectionClass::getReflectionConstants() without checking whether it provides the same result.getConstants() returns name/value pairs; getReflectionConstants() returns descriptor objects.

Key Takeaways


  • Returns a name-to-value array of class constants visible through reflection.
  • This method returns names mapped directly to constant values; descriptors require getReflectionConstants().
  • The returned metadata may be false, null, an array, or a Reflection object depending on the requested property.

The examples demonstrate where ReflectionClass::getConstants 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.