Friday, December 12, 2025
HomeLanguagesPHP | ReflectionClass getReflectionConstant() Function

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:




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

RELATED ARTICLES

Most Popular

Dominic
32445 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6814 POSTS0 COMMENTS
Nicole Veronica
11952 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12029 POSTS0 COMMENTS
Shaida Kate Naidoo
6949 POSTS0 COMMENTS
Ted Musemwa
7199 POSTS0 COMMENTS
Thapelo Manthata
6895 POSTS0 COMMENTS
Umr Jansen
6882 POSTS0 COMMENTS