PHP Magic Constants
In this page:
What Are Magic Constants?
Magic constants are special identifiers, always wrapped in double underscores like __LINE__, whose value changes depending on exactly where in the code they're written. Unlike a regular constant defined once with define(), PHP resolves a magic constant fresh at each location it appears.
Example: What Are Magic Constants?
<?php
echo __LINE__;
?>
Login to try C/C++/Java/PHP code in the editor
__LINE__, __FILE__ and __DIR__
__LINE__ gives the current line number and __FILE__ the full path of the file being executed, which together are the backbone of quick debug logging. __DIR__ gives just the containing directory — commonly used to build reliable include paths that don't depend on the current working directory.
Example: __LINE__, __FILE__ and __DIR__
<?php
echo __LINE__ . "\n";
echo __FILE__ . "\n";
echo __DIR__;
?>
Login to try C/C++/Java/PHP code in the editor
__FUNCTION__ and __METHOD__
__FUNCTION__ returns the name of the function it's written inside, while __METHOD__ returns the class-qualified form, ClassName::methodName, when used inside a class method. This distinction matters for logging, since __METHOD__ immediately tells you which class the log line came from.
Example: __FUNCTION__ and __METHOD__
<?php
function greet() {
echo __FUNCTION__ . "\n";
}
class Greeter {
function hello() {
echo __METHOD__;
}
}
greet();
(new Greeter())->hello();
?>
Login to try C/C++/Java/PHP code in the editor
__CLASS__ and __NAMESPACE__
__CLASS__ gives the fully-qualified name of the class it's written in — useful inside a trait or parent class where you want the actual instantiated class, not the one the code is physically defined in. __NAMESPACE__ returns the current namespace, handy for building namespace-relative class names.
Example: __CLASS__ and __NAMESPACE__
<?php
namespace App;
class Greeter {
function whoAmI() {
echo __CLASS__ . " in " . __NAMESPACE__;
}
}
(new Greeter())->whoAmI();
?>
Login to try C/C++/Java/PHP code in the editor
Practical Uses in Debugging and Logging
Combining several magic constants into one log message — file, line, and function — gives you an exact source location for every log entry without hardcoding anything, so the log stays accurate even after the file is renamed or the function is moved.
Example: Practical Uses in Debugging and Logging
<?php
function logMessage($msg) {
echo "[" . __FILE__ . ":" . __LINE__ . "] " . __FUNCTION__ . ": $msg";
}
logMessage("Something happened");
?>
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 24 topics to unlock
0/24 topics done
Complete these topics first:
- PHP Date & Time
- PHP Math Functions
- PHP JSON Handling
- PHP XML Handling
- PHP cURL Introduction
- PHP REST API Basics
- PHP Composer & Packages
- PHP Autoloading
- PHP Design Patterns
- PHP MVC Architecture
- PHP Security Best Practices
- PHP Performance Optimization
- PHP 8 New Features
- PHP Type Declarations
- PHP Match Expression Advanced
- PHP Fibers
- PHP Attributes
- PHP Magic Constants
- PHP Include & Require
- PHP Iterables
- PHP SimpleXML Parser
- PHP SimpleXML Get
- PHP XML Expat Parser
- PHP DOM Parser