PHP Magic Constants
PHP has a small set of special constants whose value depends on where you write them. They start and end with two underscores — __LINE__, __FUNCTION__, __CLASS__ and so on — and are filled in by the compiler. They are called "magic" because, unlike normal constants, the same name gives a different value in different places.
The full list
| Magic constant | Value |
|---|---|
__LINE__ |
current line number in the file |
__FILE__ |
full path and name of the current file |
__DIR__ |
directory of the current file (same as dirname(__FILE__)) |
__FUNCTION__ |
name of the current function |
__CLASS__ |
name of the current class, including its namespace |
__METHOD__ |
class and method name, e.g. Cart::add |
__TRAIT__ |
name of the current trait |
__NAMESPACE__ |
name of the current namespace (empty string if none) |
__PROPERTY__ |
name of the current property inside a property hook (PHP 8.4) |
ClassName::class |
fully qualified class name as a string |
Magic constants are case-insensitive (__line__ works), but everyone writes them in upper case.
__LINE__
__LINE__ is the line number on which it appears. It's mostly used in debug messages so you can find where something happened.
<?php
echo "This is line ", __LINE__, "\n";
function debug(string $msg, int $line): void {
echo "[line $line] $msg\n";
}
debug("Starting import", __LINE__);
debug("Import finished", __LINE__);This is line 2
[line 8] Starting import
[line 9] Import finished
Notice that we pass __LINE__ as an argument: inside debug() it would always report the line of debug() itself, not the caller.
__FUNCTION__, __CLASS__ and __METHOD__
These tell code where it is running. They are great for logging and for error messages that say exactly which method failed.
<?php
function calculateTotal(): string {
return __FUNCTION__;
}
class Order {
public function ship(): void {
echo "Class: ", __CLASS__, "\n";
echo "Function: ", __FUNCTION__, "\n";
echo "Method: ", __METHOD__, "\n";
}
}
echo calculateTotal(), "\n";
(new Order())->ship();calculateTotal
Class: Order
Function: ship
Method: Order::ship
Note: Inside an anonymous function,
__FUNCTION__gives a generated name. Since PHP 8.4 it looks like{closure:/path/to/file.php:17}— it includes the file and line where the closure was defined, so it differs from server to server.
__NAMESPACE__ and ::class
In a file with a namespace, __NAMESPACE__ holds its name, and __CLASS__ includes the namespace too. The ::class syntax turns any class name into its fully qualified string — handy for logging, factories and error messages, without typing the name as a string.
<?php
namespace Shop\Billing;
class Invoice {
public function describe(): string {
return __METHOD__;
}
}
echo "Namespace: ", __NAMESPACE__, "\n";
echo "Class: ", Invoice::class, "\n";
echo "Method: ", (new Invoice())->describe(), "\n";
echo "Built-in: ", \DateTime::class, "\n";Namespace: Shop\Billing
Class: Shop\Billing\Invoice
Method: Shop\Billing\Invoice::describe
Built-in: DateTime
__TRAIT__
Inside a trait, __TRAIT__ gives the trait's name, while __CLASS__ gives the class that uses the trait:
<?php
trait Greets {
public function hello(): string {
return "Hello from " . __TRAIT__ . " used in " . __CLASS__;
}
}
class Robot {
use Greets;
}
echo (new Robot())->hello(), "\n";Hello from Greets used in Robot
__FILE__ and __DIR__
These two return absolute paths on the server, so their exact text depends on where the file lives. Their most common use is building reliable paths to other files, independent of the directory the script was started from:
<?php
// in /var/www/app/public/index.php
require __DIR__ . '/../config/settings.php'; // /var/www/app/config/settings.php
echo basename(__FILE__); // index.phpTip: Always use
__DIR__ . '/file.php'inrequire/includestatements. A plain relative path like'config.php'is resolved against the current working directory, which can differ depending on how the script was started.
Test yourself
Inside the method ship() of class Order, what does __METHOD__ contain?
shipOrderOrder::shipOrder->ship
Inside the plain function calculateTotal(), what does __FUNCTION__ contain?
- An empty string
calculateTotalmain::calculateTotal- The current line number
Why is require __DIR__ . '/config.php'; preferred over require 'config.php';?
- It is faster to type
- It always resolves relative to the current file, not the working directory
__DIR__caches the file- Plain paths are not allowed in PHP 8