Open In App

PHP | ReflectionClass getReflectionConstant() Function

Last Updated : 30 Nov, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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:




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


Output:

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

Program 2:




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


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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads