Home > Article > Backend Development > How does a PHP function return a class constant name?
PHP provides the get_class_constant() function, which can return the constant name of the specified class: specify the class name or instance to obtain the constant name. Enter the constant name to be retrieved.
#How does a PHP function return a class constant name?
PHP provides a convenient function get_class_constant()
, which can return the specified constant name of a class.
Syntax:
string get_class_constant(object|string $class, string $constant_name)
Where:
$class
: Can be a class name or an instance of a class. $constant_name
: The constant name to be returned. Practical case:
The following is an example of how to use the get_class_constant()
function:
class MyClass { const MY_CONSTANT = 'My constant value'; } $constant_name = get_class_constant('MyClass', 'MY_CONSTANT'); echo $constant_name; // 输出: MY_CONSTANT
Note:
If the specified constant does not exist or $class
is not a valid class name, this function will return null
.
The above is the detailed content of How does a PHP function return a class constant name?. For more information, please follow other related articles on the PHP Chinese website!