Magic Constants
Magic constants are a small set of special, built-in constants whose value depends on where in your code they appear, not on anything you assign. They all follow the same visual pattern: two leading underscores, an all-caps word, two trailing underscores — __LINE__, __FILE__, __FUNCTION__, and a handful of others. PHP resolves each one at compile time to information about the current line, file, function, class, or namespace, which makes them invaluable for logging, debugging, and writing reusable error messages that describe exactly where something went wrong.
__LINE__ and __FILE__
__LINE__ expands to the current line number in the source file, and __FILE__ expands to the full, absolute path of the file being executed (following any symlinks). Both are resolved by the parser before the script runs, so their value never changes based on how the function containing them was called.
Locating yourself in a file
<?php echo 'This is line ' . __LINE__ . "\n"; echo 'This file lives at ' . __FILE__ . "\n";
This is line 2 This file lives at /var/www/app/scripts/report.php
__DIR__
__DIR__ is simply the directory portion of __FILE__, without a trailing slash. It was added specifically to make require/ include paths reliable regardless of which directory a script was invoked from — a very common source of "file not found" bugs before it existed.
Reliable includes with __DIR__
<?php require __DIR__ . '/config/database.php'; require __DIR__ . '/../helpers/format.php';
__FUNCTION__, __CLASS__, and __METHOD__
These three describe the name of the enclosing function, class, or method. __FUNCTION__ gives the bare function name; __CLASS__ gives the class name (including its namespace) when used inside a class body; __METHOD__ combines both as ClassName::methodName when used inside a method, which is often more useful on its own for logging than __CLASS__ and __FUNCTION__ pieced together.
Identifying the current function and class
<?php
function calculateTotal() {
echo __FUNCTION__ . "\n";
}
class InvoiceService
{
public function generate()
{
echo __CLASS__ . "\n";
echo __METHOD__ . "\n";
}
}
calculateTotal();
(new InvoiceService())->generate();calculateTotal InvoiceService InvoiceService::generate
__NAMESPACE__
__NAMESPACE__ expands to the name of the current namespace as a string, or an empty string when the code is in the global namespace. It is mostly used inside library code that needs to build fully qualified class names dynamically, without hardcoding the namespace the file happens to live in.
Reading the current namespace
<?php namespace App\Billing; echo __NAMESPACE__;
App\Billing
Practical use: a reusable debug logger
Magic constants shine when combined into a small helper that tags every log line with exactly where it was written from, which saves you from typing the file name and line number by hand every time you add a temporary debug statement.
A tiny logging helper
<?php
function debugLog(string $message): void
{
$location = basename(__FILE__) . ':' . __LINE__;
echo "[{$location}] {$message}\n";
}
debugLog('Cache miss for user 42');[report.php:9] Cache miss for user 42
Summary table
Constant | Resolves to |
|---|---|
| Current line number in the file |
| Full absolute path of the current file |
| Directory of the current file (no trailing slash) |
| Name of the enclosing function |
| Name of the enclosing class, with namespace |
| Class and method name together, as |
| Name of the current namespace |
Magic constants are case-insensitive in name only historically; by convention everyone writes them fully uppercase.
They cannot be reassigned or redefined — attempting define('LINE', 1) is simply meaningless, since these are parser-level, not stored, values.
Outside of any function,
__FUNCTION__and__METHOD__both resolve to an empty string.