Magic Constants in PHP

Magic constants are special types of predefined constants in PHP whose values change depending on where they are used in the script.

They provide useful information about your code, such as the current file name, directory name, function, class, and namespace where the constant is invoked.

Magic constant name begins and ends with double underscores (__). They are case-insensitive, meaning that __Line__ and __line__, both are valid magic constants and perform the same task.

You can use them directly wherever required in your program code. Their values are automatically provided based on the used in the code. Let’s take an example on it.

Example :

<?php
echo __LINE__;
echo "\n";
echo "You are using " . __FILE__. "file";
?>
Output:
       2
       You are using D:\xampp\htdocs\PHP_Project\index.phpfile

In the above example, __LINE__ and __FILE__ are the magic constants. The __LINE__ magic constant will display the current line number of the program file. The __FILE__ will display the file name and complete file path of the current file.

List of Magic Constants in PHP


There are eight magical constants in PHP. Here’s a list of the most commonly used magic constants in PHP:

  • __LINE__
  • __FILE__
  • __DIR__
  • __FUNCTION__
  • __CLASS__
  • __TRAIT__
  • __METHOD__
  • __NAMESPACE__

__LINE__

The __LINE__ magic constant returns the current line number of the PHP program file where it’s used. In other words, it displays the current line number of the file being executed.

Example 2:

<?php
echo "Hello";
echo "\n";
echo "This is line number " . __LINE__;
?>
Output:
       Hello
       This is line number 4

This magic constant is especially useful when you want to debug your code. You can include line numbers in your error messages or logs. For example:

<?php
function divide($a, $b) {
  if ($b == 0) {
     // Log the error with the line number.
        echo "Error: Division by zero on line " . __LINE__;
  } else {
       return $a / $b;
    }
}
// Call the function with passing an argument zero as a divisor to generate the error.
divide(10, 0);
?>
Output:
      Error: Division by zero on line 5

__FILE__

The __FILE__ magic constant returns the file name and the complete file path of the current file. In other words, this constant displays the full path of the current file with the file name.

Example 3:

<?php
echo "The file is located at " . __FILE__;
?>
Output:
      The file is located at D:\xampp\htdocs\PHP_Project\index.php

This magic constant is useful when you want to know the absolute path of the file being executed.

__DIR__

The __DIR__ magic constant returns the directory (path of file without its name) of the file. In other words, it returns the absolute directory in which the file is located.

Example 4:

<?php
echo "The directory is " . __DIR__;
?>
Output:
       The directory is D:\xampp\htdocs\PHP_Project

__FUNCTION__

The __FUNCTION__ constant returns the name of the function inside which it is called.

Example 5:

<?php
// Declare a function.
function testFunction() {
echo "The function name is " . __FUNCTION__;
}
testFunction(); // Calling function.
?>
Output:
      The function name is testFunction

__CLASS__

The magic constant __CLASS__ returns the name of the class in which it is defined. It is useful when you want to log or debug class-based code.

Example 6:

<?php
class MyClass {
  public function displayClass() {
     echo "The class name is " . __CLASS__;
  }
}
// Creating an instance of class.
$obj = new MyClass();

// Calling function.
$obj -> displayClass();
?>
Output:
       The class name is MyClass

__METHOD__

The __METHOD__ constant returns the name of the method inside which it is called, including the class name.

Example 7:

<?php
class MyClass {
  public function displayMethod() {
     echo "The method name is " . __METHOD__;
  }
}
$obj = new MyClass();
$obj->displayMethod();
?>
Output:
      The method name is MyClass::displayMethod

__TRAIT__

The __TRAIT__ magic constant returns the name of the trait, including namespace.

Example 8:

<?php
trait MyTrait {
   public function displayTrait() {
      echo "Name of trait is " . __TRAIT__;
   }
}
class MyClass {
   use MyTrait;
}
$obj = new MyClass();
$obj->displayTrait();
?>
Output:
      Name of trait is MyTrait

__NAMESPACE__

The __NAMESPACE__ is the eighth and final magic constant that returns the name of the current namespace.

Example 9:

<?php
namespace MyNamespace;

function displayNamespace() {
   echo "Name of namespace is " . __NAMESPACE__;
}
displayNamespace();
?>
Output:
       Name of namespace is MyNamespace

Best Practices for Using Magic Constants


There are some important points that you should remember while using magic constants in your PHP program. They are as:

  • Use magic constants such as __LINE__, __FILE__, and __DIR__ for debugging purposes. These magic constants can print out log messages to quickly identify issues in your code.
  • Avoid overuse of magic constants because overusing them can clutter your code, especially if you insert them in places where they aren’t necessary.
  • Always use __NAMESPACE__ constant in a namespaced environment to avoid collisions with global symbols.
  • Use __method__ constant for method logging while working in object-oriented programming (OOP). This is because the __METHOD__ is a great way to track which methods are being called, especially when debugging large application programs.

Magic constants in PHP are powerful features for debugging, logging, and providing contextual information about your program code. You should use when you need them in your code. I hope that you will have understood the basic points of all eight magic constants with examples.