Friday, October 24, 2025
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
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS