php8.5
Home/ Manual/ reflection / reflectionclass/ ReflectionClass::getReflectionConstants

ReflectionClass::getReflectionConstants

PHP function Edit on GitHub ✎

(PHP 7 >= 7.1.0, PHP 8)

Gets class constants

Description

ReflectionClass::getReflectionConstants(int|null $filter = null): array

Retrieves reflected constants.

Parameters

filter

The optional filter, for filtering desired constant visibilities. It's configured using the ReflectionClassConstant constants, and defaults to all constant visibilities.

Return Values

An array of ReflectionClassConstant objects.

Changelog

VersionDescription
8.0.0filter has been added.

Examples

Basic ReflectionClass::getReflectionConstants() example

php
<?php
class Foo {
    public    const FOO  = 1;
    protected const BAR  = 2;
    private   const BAZ  = 3;
}

$foo = new Foo();

$reflect = new ReflectionClass($foo);
$consts  = $reflect->getReflectionConstants();

foreach ($consts as $const) {
    print $const->getName() . "\n";
}

var_dump($consts);

?>

The above example will output something similar to:

output
FOO
BAR
BAZ
array(3) {
  [0]=>
  object(ReflectionClassConstant)#3 (2) {
    ["name"]=>
    string(3) "FOO"
    ["class"]=>
    string(3) "Foo"
  }
  [1]=>
  object(ReflectionClassConstant)#4 (2) {
    ["name"]=>
    string(3) "BAR"
    ["class"]=>
    string(3) "Foo"
  }
  [2]=>
  object(ReflectionClassConstant)#5 (2) {
    ["name"]=>
    string(3) "BAZ"
    ["class"]=>
    string(3) "Foo"
  }
}

See Also

  • ReflectionClass::getReflectionConstant
  • ReflectionClassConstant

Source: reference/reflection/reflectionclass/getreflectionconstants.xml · from the official PHP manual (php/doc-en)