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.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read