Monday, May 25, 2026
HomeLanguagesPHP | ReflectionClass getConstant() Function

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:




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


Output:

CSE

Program 2:




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


Output:

neveropen
GFG
gfg

Reference: https://www.php.net/manual/en/reflectionclass.getconstant.php

RELATED ARTICLES

Most Popular

Dominic
32514 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6892 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12107 POSTS0 COMMENTS
Shaida Kate Naidoo
7016 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6975 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS