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' )); ?> |
neveropen GFG gfg
Reference: https://www.php.net/manual/en/reflectionclass.getconstant.php