PHP Magic Constants

Special constants that change based on context

✨ What are Magic Constants?

Magic constants are predefined PHP constants that change their value depending on where they are used. They start and end with double underscores and provide useful information about code location.


<?php
// Magic constant showing current line
echo "This is line: " . __LINE__;
// Outputs: This is line: 3
?>
                                    

Output:

This is line: 3

Common Magic Constants

📄

__LINE__

Returns the current line number in the file where it's used, helpful for debugging.

<?php
echo __LINE__;
// Outputs: 2
?>
📁

__FILE__

Returns the full path and filename of the current file being executed.

<?php
echo __FILE__;
// Outputs: /path/to/file.php
?>
📂

__DIR__

Returns the directory path of the current file, useful for including other files.

<?php
echo __DIR__;
// Outputs: /path/to/directory
?>
⚙️

__FUNCTION__

Returns the name of the current function where the constant is used.

<?php
function test() {
    echo __FUNCTION__;
}
?>

🔹 __LINE__ and __FILE__

These magic constants help you track code location:

<?php
echo "Current file: " . __FILE__;
echo "<br>";
echo "Current line: " . __LINE__;
echo "<br>";
echo "This is line: " . __LINE__;

// Useful for debugging
function debugInfo() {
    echo "Error in file: " . __FILE__ . " on line: " . __LINE__;
}
debugInfo();
?>

Output:

Current file: /var/www/html/test.php

Current line: 3

This is line: 5

Error in file: /var/www/html/test.php on line: 9

🔹 __DIR__ for File Paths

Use __DIR__ to include files relative to the current directory:

<?php
// Get current directory
echo "Current directory: " . __DIR__;
echo "<br>";

// Include files using __DIR__
// require_once __DIR__ . '/config.php';
// include __DIR__ . '/functions.php';

// Build file paths
$configPath = __DIR__ . '/config/database.php';
echo "Config path: " . $configPath;
?>

Output:

Current directory: /var/www/html

Config path: /var/www/html/config/database.php

🔹 __FUNCTION__ and __METHOD__

Track which function or method is currently executing:

<?php
function greet() {
    echo "Inside function: " . __FUNCTION__;
}
greet();

echo "<br>";

class User {
    public function login() {
        echo "Inside method: " . __METHOD__;
        echo "<br>";
        echo "Class name: " . __CLASS__;
    }
}

$user = new User();
$user->login();
?>

Output:

Inside function: greet

Inside method: User::login

Class name: User

🔹 __CLASS__ and __NAMESPACE__

Get information about classes and namespaces:

<?php
namespace MyApp\Models;

class Product {
    public function getInfo() {
        echo "Class: " . __CLASS__;
        echo "<br>";
        echo "Namespace: " . __NAMESPACE__;
        echo "<br>";
        echo "Method: " . __METHOD__;
    }
}

$product = new Product();
$product->getInfo();
?>

Output:

Class: MyApp\Models\Product

Namespace: MyApp\Models

Method: MyApp\Models\Product::getInfo

🔹 __TRAIT__ Magic Constant

Get the name of the trait being used:

<?php
trait Logger {
    public function log() {
        echo "Trait name: " . __TRAIT__;
    }
}

class Application {
    use Logger;
}

$app = new Application();
$app->log();
?>

Output:

Trait name: Logger

🔹 Practical Usage Example

Combining magic constants for debugging and logging:

<?php
function logError($message) {
    $log = "[" . date("Y-m-d H:i:s") . "] ";
    $log .= "File: " . __FILE__ . " ";
    $log .= "Line: " . __LINE__ . " ";
    $log .= "Message: " . $message;
    echo $log;
}

// Usage
logError("Database connection failed");
echo "<br><br>";

// Another example
class Database {
    public function connect() {
        echo "Connecting from: " . __METHOD__;
        echo "<br>";
        echo "In file: " . __FILE__;
    }
}

$db = new Database();
$db->connect();
?>

Output:

[2025-02-10 14:30:45] File: /var/www/html/app.php Line: 6 Message: Database connection failed


Connecting from: Database::connect

In file: /var/www/html/app.php

🧠 Test Your Knowledge

Which magic constant returns the current line number?