![]() |
VOOZH | about |
Magic constants in PHP are special built-in constants that provide information about the current state of the script, such as the file name, line number, function name, class name, and more. They always start and end with double underscores (__) and are automatically used in by PHP.
Here’s a list of all major magic constants supported in PHP:
Magic Constant | Description |
|---|---|
__LINE__ | Current line number of the file |
__FILE__ | Full path and filename of the file |
__DIR__ | Directory of the file |
__FUNCTION__ | Name of the current function |
__CLASS__ | Name of the current class |
__TRAIT__ | Name of the current trait |
__METHOD__ | Name of the current method |
__NAMESPACE__ | Name of the current namespace |
ClassName::class | It gives the complete name of the class, including its namespace, as a text value (string). |
Note: ClassName::class is not a magic constant, but a special class name resolution feature introduced in PHP 5.5. It is widely used for type referencing and dependency injection in modern PHP applications.
This magic constant returns the current line number of the file. If you use this magic constant in your program file somewhere, then this constant will display the line number during compile time.
Syntax:
.__line__Now, let us understand with the help of the example:
The Line number is : 2
This magic constant return the full path of the executed file with the name of the file.
Syntax:
.__file__Now, let us understand with the help of the example:
The file name is : /home/guest/sandbox/Solution.php
This magic constant return the directory of the executed file.
Syntax:
.__dir__Now, let us understand with the help of the example:
The directory is : /home/guest/sandbox
This magic constant return the name of the function where this magic constant is included.
Syntax:
.__function__Now, let us understand with the help of the example:
The function name is : Geeks
This magic constant return the name of the class where this magic constant is included.
Syntax:
__class__Now, let us understand with the help of the example:
Geeks
This magic constant return the method name where this magic constant is included.
Syntax:
__method__Now, let us understand with the help of the example:
Company::GeeksforGeeks
This magic constant return the current namespace where this magic constant is included.
Syntax:
__namespace__Now, let us understand with the help of the example:
GeeksforGeeks
This magic constant return the trait name where this magic constant is included.
Syntax:
__trait__Now, let us understand with the help of the example:
GeeksforGeeks
This magic constant return the fully qualified class name.
Syntax:
ClassName::classNow, let us understand with the help of the example:
Computer_Sciecnec_Portal\Geeks
Magic constants in PHP are the tools that help developers get useful information about their code, such as the file name, line number, function name, and more. They are automatically set by PHP and change based on where they are used, making them very helpful for debugging, logging, and organizing large projects. By understanding and using magic constants properly, you can write cleaner, more informative, and easier-to-maintain PHP code.