PHP | ReflectionClass getConstant() Function

The ReflectionClass::getConstant() function is an inbuilt function in PHP which is used to return the value of the defined constant.

Syntax:

mixed ReflectionClass::getConstant( string $name )

Parameters: This function accepts a parameter Name which is the name of the defined constant.

Return Value: This function returns the value of the defined constant.

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



Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Declaring a class named as Department
class Department {
      
    // Defining a constant
    const First = "CSE";
      
}
  
// Using the ReflectionClass() function 
// over the Department class
$A = new ReflectionClass('Department');
  
// Calling the getConstant() function over
// First constant
$a = $A->getConstant('First');
  
// Getting the value of the defined constant
print_r($a);
?>

chevron_right


Output:

CSE

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
    
// Declaring a class named as Company
class Company {
        
    // Defining a constant
    const First = "GeeksforGeeks";
    const Second = "GFG";
    const Third = "gfg";
        
}
    
// Using the ReflectionClass() function 
// over the Company class
$A = new ReflectionClass('Company');
    
// Calling the getConstant() function over
// the defined constants and
// getting the value of the defined constants
print_r($A->getConstant('First'));
echo("\n");
print_r($A->getConstant('Second'));
echo("\n");
print_r($A->getConstant('Third'));
?>

chevron_right


Output:

GeeksforGeeks
GFG
gfg

Reference: https://www.php.net/manual/en/reflectionclass.getconstant.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.