← Back to PHP Course | Chapter 14: Advanced PHP | Lesson 18 of 24

PHP Magic Constants

Magic constants are special built-in names that change depending on where you use them, like a badge that always shows the current room. They can tell you the current line, file, or function.
Syntax
php
__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
<?php
// Print `__LINE__` to the output
echo __LINE__;
?>

__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
<?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__;
?>

__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
<?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();
?>

__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
<?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();
?>

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
<?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");
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Expecting __FILE__ to give a relative path, when it returns the full path of the file.
  2. Using __CLASS__ outside a class, where it gives an empty string.
  3. Confusing __DIR__ (folder of the current file) with the current working directory.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.