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.
- Their values change automatically based on where they are used in the code (e.g., current line, file, function, class).
- They are built-in PHP values and cannot be created, modified, or deleted by the user.
- Helpful in debugging, logging, and reflecting code structure without manual input.
- Magic Constants are not Case Sensitive.
List of PHP Magic Constants
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.
Implementation of PHP Magic Constants
1. __line__
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:
PHP
<?php
echo "The Line number is : ". __line__;
?>
OutputThe Line number is : 2
2. __file__
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:
PHP
<?php
echo "The file name is : ". __file__;
?>
OutputThe file name is : /home/guest/sandbox/Solution.php
3. __dir__
This magic constant return the directory of the executed file.
Syntax:
.__dir__
Now, let us understand with the help of the example:
PHP
<?php
echo "The directory is : ". __dir__;
?>
OutputThe directory is : /home/guest/sandbox
4. __function__
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:
PHP
<?php
function Geeks(){
echo "The function name is : ". __function__;
}
Geeks();
?>
OutputThe function name is : Geeks
5. __class__
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:
PHP
<?php
class Geeks
{
public function getClassName(){
return __class__;
}
}
$obj = new Geeks();
echo $obj->getClassName();
?>
6. __method__
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:
PHP
<?php
class Company
{
public function GeeksforGeeks(){
return __method__;
}
}
$obj = new Company();
echo $obj->GeeksforGeeks();
?>
OutputCompany::GeeksforGeeks
7. __namespace__
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:
PHP
<?php
namespace GeeksforGeeks;
class Company {
public function gfg() {
return __namespace__;
}
}
$obj = new Company();
echo $obj->gfg();
?>
8. __trait__
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:
PHP
<?php
trait GeeksforGeeks{
function gfg(){
echo __trait__;
}
}
class Company{
use GeeksforGeeks;
}
$a = new Company;
$a->gfg();
?>
9. ClassName::class
This magic constant return the fully qualified class name.
Syntax:
ClassName::class
Now, let us understand with the help of the example:
PHP
<?php
namespace Computer_Sciecnec_Portal;
class Geeks{ }
echo Geeks::class;//Classname::class
?>
OutputComputer_Sciecnec_Portal\Geeks
Best Practices for Using Magic Constants
- Use magic constants for debugging and error reporting.
- Avoid using them unnecessarily in production output.
- Combine them with logging tools for better traceability.
- Always keep code context clear when using them, especially in large scripts or frameworks.
Conclusion
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.
Explore
Basics
Array
OOPs & Interfaces
MySQL Database
PHP Advance