PHPMagic Constants

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
<?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
<?php
require __DIR__ . '/config/database.php';
require __DIR__ . '/../helpers/format.php';
Why not a relative path directly?
`require 'config/database.php'` depends on PHP's current working directory at the moment the script runs, which can differ between a command-line invocation, a cron job, and a web server request. `__DIR__` always resolves relative to the file containing it, so the include works no matter how the script was launched.
__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
<?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
<?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
<?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
__LINE__ reports where it is written, not where the function was called from
Because `__LINE__` is resolved when the parser sees it, putting it inside a helper function like `debugLog()` always reports the line inside that helper, not the line of the code that called it. If you need the caller's line number, you have to pass it in explicitly (often via `debug_backtrace()`), rather than relying on a magic constant to reach across function boundaries.
Summary table

Constant

Resolves to

__LINE__

Current line number in the file

__FILE__

Full absolute path of the current file

__DIR__

Directory of the current file (no trailing slash)

__FUNCTION__

Name of the enclosing function

__CLASS__

Name of the enclosing class, with namespace

__METHOD__

Class and method name together, as Class::method

__NAMESPACE__

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.

Tip
Use __METHOD__ (not __CLASS__ . '::' . __FUNCTION__) whenever you are logging from inside a method — it is shorter, and it stays correct automatically if the method is later renamed.