PHP has nine predefined constants that change value based on their context, which is why they are referred to as “magic constants.”
These magic constants are enclosed with double underscores at both the beginning and the end, except for the ClassName::class
constant.
Here are the magic constants, along with their descriptions and examples:
Constant |
Description |
|
__CLASS__ |
If used within a class, it returns the name of the class. |
|
__DIR__ |
The file’s directory. |
|
__FILE__ |
The complete path along with the file name. |
|
__FUNCTION__ |
If used within a function, it returns the name of the function. |
|
__LINE__ |
The line number currently being executed. |
|
__METHOD__ |
If used within a function that belongs to a class, it returns both the class name and the function name. |
|
__NAMESPACE__ |
If used within a namespace, it returns the name of the namespace. |
|
__TRAIT__ |
If used within a trait, it returns the name of the trait. |
|
ClassName::class |
It provides the name of the specified class and, if applicable, the name of the namespace. |
Note: The magic constants are case-insensitive, so |