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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Declaring a class named as Company
class Company {
      
    // Defining some constants
    const First = "GeeksforGeeks";
    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);
?>

chevron_right


Output:

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

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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);
?>

chevron_right


Output:

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

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




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.