PHP Lesson 16 – Magic Constants

Last updated on October 29th, 2023

What is a “magic constant” in PHP?

In PHP, “magic constants” are predefined special values that can change depending on where and how they are used in your code. These constants start and end with two underscores (__) and can provide valuable information about the code’s context during its execution. You can use the magic constants to get information about your PHP script, such as the current line number, file name, directory, or function name.

List of magic constants in PHP

This constant returns the current line number in the code where it is used. For example, if you have a piece of code that uses LINE, it will return the line number where that code is written.

When used, this constant provides the complete path and filename of the PHP script where it’s used. It’s particularly useful for tasks like logging or debugging, helping you identify the exact location of a file within your directory structure.

This one gives you the directory path of the current file. It can be especially handy when you want to include files from the same directory, as it helps create more reliable and flexible code.

If you use this magic constant inside a function, it returns the name of the function itself. It’s beneficial for debugging or logging functions, allowing you to keep track of which functions are being executed.

When used inside a class, it returns the name of the class. This can be very useful for tasks like autoloading classes or for creating more dynamic and flexible code structures.

Similar to CLASS, this constant returns the name of the trait in which it’s used. Traits are a way to share methods among classes in PHP, and knowing which trait is being used can be crucial for managing code organization.

If used inside a class, this constant provides the name of the method currently being executed. It’s particularly helpful for debugging or logging methods within classes.

This constant returns the name of the current namespace. Namespaces are essential for organizing your code, especially in larger projects, and knowing the current namespace can help you manage your code’s structure more effectively.

Simple examples of using magic constants

Using __LINE__:

echo "This is line " . __LINE__;

Output: This is line 2

In this example, the __LINE__ magic constant is used to display the current line number. Since the echo statement is on line 2, the output will show “This is line 2”.

Using __FILE__:

echo "The filename is " . __FILE__;

Output: The filename is /path/to/your/file.php

The __FILE__ magic constant displays the full path and filename of the current script. In this case, it will show the actual path to your PHP file (for example: C:\xampp\htdocs\test.php).

Using __DIR__:

echo "The current directory is " . __DIR__;

Output: The current directory is /path/to/your/directory

The __DIR__ magic constant provides the directory path of the current file (for example: C:\xampp\htdocs\). It is helpful when you want to include other files from the same directory.

Using __FUNCTION__:

function myFunctionName() {
    echo "The function name is " . __FUNCTION__;

Output: The function name is myFunctionName

Here, the __FUNCTION__ magic constant is used to display the name of the function in which it is used. In this case, it prints the name of the function “myFunctionName”.