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 classCompany {      Â    // Defining some constants     constFirst = "zambiatek";     constSecond = "GFG"; }  Â// Using the ReflectionClass() function  // over the Company class $A= newReflectionClass('Company');  Â// Calling the getConstants() function $a= $A->getConstants();  Â// Getting an array of the constants print_r($a); ?>  | 
Output:
Array
(
    [First] => zambiatek
    [Second] => GFG
)
Program 2:
| <?php  Â// Declaring a class named as Departments classDepartments {      Â    // Defining some constants     constFirst = "CSE";     constSecond = "ECE";     constThird = "EE";     constFourth = "Mechanical"; }  Â// Using the ReflectionClass() function  // over the Departments class $A= newReflectionClass('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
 
				 
					


