PHP | ReflectionClass getReflectionConstant() Function

The ReflectionClass::getReflectionConstant() function is an inbuilt function in PHP which is used to return the ReflectionClassConstant of the specified class’s property.

Syntax:

ReflectionClassConstant ReflectionClass::getReflectionConstant( string $name )

Parameters: This function accepts a single parameter name which is the name of the class constant.

Return Value: This function returns the ReflectionClassConstant of the specified class’s property.

Below programs illustrate the ReflectionClass::getReflectionConstant() 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 getReflectionConstant() function
// over the constant 'First'
$a = $A->getReflectionConstant('First');
  
// Getting the ReflectionClassConstant
print_r($a);
?>

chevron_right


Output:

ReflectionClassConstant Object
(
    [name] => First
    [class] => Company
)

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
   
// Declaring a user-defined class Departments
class Departments {
       
    // Defining some constants
    const D1 = "EE";
    const D2 = "Civil";
    const D3 = "CSE";
    const D4 = "IT";
}
   
// Using the ReflectionClass() function 
// over the Departments class
$A = new ReflectionClass('Departments');
   
// Calling the getReflectionConstant() function
// over specified constants
$a1 = $A->getReflectionConstant('D1');
$a2 = $A->getReflectionConstant('D2');
$a3 = $A->getReflectionConstant('D3');
$a4 = $A->getReflectionConstant('D4');
   
// Getting the ReflectionClassConstant
print_r($a1);
print_r($a2);
print_r($a3);
print_r($a4);
?>

chevron_right


Output:

ReflectionClassConstant Object
(
    [name] => D1
    [class] => Departments
)
ReflectionClassConstant Object
(
    [name] => D2
    [class] => Departments
)
ReflectionClassConstant Object
(
    [name] => D3
    [class] => Departments
)
ReflectionClassConstant Object
(
    [name] => D4
    [class] => Departments
)

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