Skip to content
SW Study Warehouse
Home › Blog
Blog

Magic Constants in PHP (__LINE__, __FILE__, __DIR__ Explained 2026)

By Pardeep Patel · November 25, 2026 · 4 min read 👁 218 Views
PhP
Disclosure: Some links in this guide are affiliate links. We may earn a commission at no extra cost to you.

Understanding magic constants in php gives you clear, actionable steps to implement or troubleshoot this topic effectively. Below you will find the complete breakdown, key rules, and practical references.

Complete Guide: Magic Constants In Php

Magic constants are the predefined constants in PHP which get changed on the basis of their use. They start with the double underscore (__) and ends with the double underscore. They are similar to other predefined constants but as they change their values with the context, they are called magic constants.

There are eight magical constants. They are case-insensitive.

  • __LINE__: Represents current line number where it is used.Represents current line number where it is used.
  • __FILE__: Represents full path and file name of the file. If it is used inside an include, a name of included file is returned.
  • __DIR__: Represents full directory path of the file. Equivalent to dirname(__file__). It does not have a trailing slash unless it is a root directory. It also resolves symbolic link.
  • __FUNCTION__: Represents the function name where it is used. If it is used outside of any function, then it will return blank.
  • __CLASS__: Represents the function name where it is used. If it is used outside of any function, then it will return blank.
  • __TRAIT__: Represents the trait name where it is used. If it is used outside of any function, then it will return blank. It includes namespace it was declared in.
  • __METHOD__: Represents the name of the class method where it is used. The method name is returned as it was declared.
  • __NAMESPACE__: Represents the name of the current namespace.
  • PHP_VERSION: The PHP version
  • PHP_INT_MAX: The PHP integer value limit
Example
<?php  
echo "<h3>Example for __LINE__</h3>";  
echo "You are at line number " . __LINE__ . "<br><br>";// print Your current line number i.e;3  
echo "<h3>Example for __FILE__</h3>";  
echo __FILE__ . "<br><br>";//print full path of file with .php extension  
echo "<h3>Example for __DIR__</h3>";  
echo __DIR__ . "<br><br>";//print full path of directory where script will be placed  
echo dirname(__FILE__) . "<br><br>"; //its output is equivalent to above one.  
echo "<h3>Example for __FUNCTION__</h3>";  
//Using magic constant inside function.  
function cash(){  
echo 'the function name is '. __FUNCTION__ . "<br><br>";//the function name is cash.  
}  
cash();  
//Using magic constant outside function gives the blank output.  
function test_function(){  
echo 'HELLO';  
}  
test_function();  
echo  __FUNCTION__ . "<br><br>";//gives the blank output.  
  
echo "<h3>Example for __CLASS__</h3>";  
class abc  
{  
public function __construct() {  
;  
}  
function abc_method(){  
echo __CLASS__ . "<br><br>";//print name of the class abc.  
}  
}  
$t = new abc;  
$t->abc_method();  
class first{  
function test_first(){  
echo __CLASS__;//will always print parent class which is first here.  
}  
}  
class second extends first  
{  
public function __construct() {  
;  
}  
}  
$t = new second;  
$t->test_first();  
echo "<h3>Example for __TRAIT__</h3>";  
trait created_trait{  
function abc(){  
echo __TRAIT__;//will print name of the trait created_trait  
}  
}  
class anew{  
use created_trait;  
}  
$a = new anew;  
$a->abc();  
echo "<h3>Example for __METHOD__</h3>";  
class meth{  
public function __construct() {  
echo __METHOD__ . "<br><br>";//print meth::__construct  
}  
public function meth_fun(){  
echo __METHOD__;//print meth::meth_fun  
}  
}  
$a = new meth;  
$a->meth_fun();  
  
echo "<h3>Example for __NAMESPACE__</h3>";  
class name{  
public function __construct() {  
echo 'This line will be printed on calling namespace';  
}  
}  
$clas_name= __NAMESPACE__ .'\name';  
$a = new $clas_name;  

echo "Current PHP Version you are using  : ".PHP_VERSION;

echo "Integer Maximum Value : ".PHP_INT_MAX;

?> 

Output

Example for __LINE__

You are at line number 5

Example for __FILE__

C:\wamp64\www\gh.php

Example for __DIR__

C:\wamp64\www C:\wamp64\www

Example for __FUNCTION__

the function name is cash HELLO

Example for __CLASS__

abc first

Example for __TRAIT__

created_trait

Example for __METHOD__

meth::__construct meth::meth_fun

Example for __NAMESPACE__

This line will be printed on calling namespace Current PHP Version you are using : 5.6.25 Integer Maximum Value : 2147483647
 

Key Takeaways & References for Magic Constants In Php

For further details, consult the Official PHP Manual or read our Basic Syntax of PHP.

Found this guide helpful? Support Team StudyWarehouse! Buy Us a Coffee

Pardeep Patel

Hi!, I am Pardeep Patel, an Indian passport holder, Traveler, Blogger, Story Writer. I completed my M-Tech (Computer Science) in 2016. I love to travel, eat different foods from various cuisines, experience different cultures, make new friends and meet other.

Leave a Reply

Your email address will not be published. Required fields are marked *