PHP Basic Tutorial
Magic constants are predefined constants that change based on their location in the code. Unlike regular constants, they do not have fixed values; instead, their values change depending on where they are used. Magic constants start and end with double underscores (__
), except for the ClassName::class constant.
Magic Constant |
Description |
---|---|
__LINE__ |
Current line number |
__FILE__ |
Full path and filename of the file |
__DIR__ |
Directory of the file |
__FUNCTION__ |
Function name |
__METHOD__ |
Class method name |
__NAMESPACE__ |
Current namespace |
__TRAIT__ |
Trait name (PHP 5.4+) |
ClassName::class |
Returns the name of the specified class and the name of the namespace, if any. |
Try it yourself
Try it yourself
__CLASS__
includes the namespace in its value (if applicable).Try it yourself
Try it yourself
Try it yourself
Try it yourself
Try it yourself