Tuesday, October 7, 2025
HomeLanguagesPHP | ReflectionClass getConstants() Function

PHP | ReflectionClass getConstants() Function

The ReflectionClass::getConstants() function is an inbuilt function in PHP which is used to return an array of the specified constant names.

Syntax:

array ReflectionClass::getConstants( void )

Parameters: This function does not accept any parameter.

Return Value: This function returns an array of the specified constant names.

Below programs illustrate the ReflectionClass::getConstants() function in PHP:

Program 1:




<?php
  
// Declaring a class named as Company
class Company {
      
    // Defining some constants
    const First = "neveropen";
    const Second = "GFG";
}
  
// Using the ReflectionClass() function 
// over the Company class
$A = new ReflectionClass('Company');
  
// Calling the getConstants() function
$a = $A->getConstants();
  
// Getting an array of the constants
print_r($a);
?>


Output:

Array
(
    [First] => neveropen
    [Second] => GFG
)

Program 2:




<?php
  
// Declaring a class named as Departments
class Departments {
      
    // Defining some constants
    const First = "CSE";
    const Second = "ECE";
    const Third = "EE";
    const Fourth = "Mechanical";
}
  
// Using the ReflectionClass() function 
// over the Departments class
$A = new ReflectionClass('Departments');
  
// Calling the getConstants() function
$a = $A->getConstants();
  
// Getting an array of the constants
print_r($a);
?>


Output:

Array
(
    [First] => CSE
    [Second] => ECE
    [Third] => EE
    [Fourth] => Mechanical
)

Reference: https://www.php.net/manual/en/reflectionclass.getconstants.php

RELATED ARTICLES

Most Popular

Dominic
32340 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6708 POSTS0 COMMENTS
Nicole Veronica
11872 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11936 POSTS0 COMMENTS
Shaida Kate Naidoo
6829 POSTS0 COMMENTS
Ted Musemwa
7090 POSTS0 COMMENTS
Thapelo Manthata
6780 POSTS0 COMMENTS
Umr Jansen
6784 POSTS0 COMMENTS