PHP Magic Constants
In this page:
__LINE__; // current line number
__FILE__; // full path of this file
__DIR__; // directory of this file
__FUNCTION__; // current function name
__METHOD__; // Class::method
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
// Print `__LINE__` to the output
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
// Print `__LINE__ . "\n"` to the output
echo __LINE__ . "\n";
// Print `__FILE__ . "\n"` to the output
echo __FILE__ . "\n";
// Print `__DIR__` to the output
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
// Define the function `greet` with no parameters
function greet() {
// Print `__FUNCTION__ . "\n"` to the output
echo __FUNCTION__ . "\n";
}
// Define the class `Greeter`
class Greeter {
// Define the function `hello` with no parameters
function hello() {
// Print `__METHOD__` to the output
echo __METHOD__;
}
}
// Call `greet()`
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
// Declare the namespace `App`
namespace App;
// Define the class `Greeter`
class Greeter {
// Define the function `whoAmI` with no parameters
function whoAmI() {
// Print `__CLASS__ . " in " . __NAMESPACE__` to the output
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
// Define the function `logMessage` taking `$msg`
function logMessage($msg) {
// Print "[" . __FILE__ . ":" . __LINE__ . "] " . __FUNCTION__ . ": $msg" to the output
echo "[" . __FILE__ . ":" . __LINE__ . "] " . __FUNCTION__ . ": $msg";
}
// Call `logMessage("Something happened")`
logMessage("Something happened");
?>
Login to try C/C++/Java/PHP code in the editor
- Expecting
__FILE__to give a relative path, when it returns the full path of the file. - Using
__CLASS__outside a class, where it gives an empty string. - Confusing
__DIR__(folder of the current file) with the current working directory.
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