PHP Magic Constants

In this tutorial, you will learn what is magic constants and how to use those in PHP.


PHP Magic Constants are predefined constants that are changed depending on the basis of their use. These constants start with double underscores(__) and end with double underscores.

Magic constants are similar to other predefine constants. But as they change their value depending on the use, they are called Magic constants.

There are nine magic constants in PHP. Among them, eights are start and end with double underscores.

  1. __LINE__
  2. __FILE__
  3. __DIR__
  4. __FUNCTION__
  5. __CLASS__
  6. __TRAIT__
  7. __METHOD__
  8. __NAMESPACE__
  9. ClassName::class

All these “magical” constants are resolved at compile-time, unlike regular constants, which are resolved at runtime. PHP magic constants are case-insensitive.

Changelog

Version Description
5.5.0 Added ::class magic constant
5.4.0 Added __TRAIT__ constant
5.3.0 Added __DIR__ and __NAMESPACE__ constan

All the PHP magic constants are defined below with an example.

1. __LINE__

The uses of this magic constant to get the line no of the file, where the constant is used.

<?php
echo "The Line number " . __LINE__ . "<br>"; 
echo "The Line number " . __LINE__ . "<br>"; 
echo "The Line number " . __LINE__ . "<br>"; 
?>

Output

The Line number 2
The Line number 3
The Line number 4

2. __FILE__

It returns the full path along with the filename of the file. If used inside an include, the name of the included file is returned.

<?php
// Displays the absolute path of this file
echo "The full path of this file is: " . __FILE__;
?>

Output

The full path of this file is: C:\xampp\htdocs\magic-constant2.php

3. __DIR__

This returns directory of the file. This is equivalent to dirname(__FILE__). This directory name does not have a trailing slash unless it is the root directory.

<?php 
//print full path of directory where script is placed 
echo __DIR__ . "<br>"; 
//below output will equivalent to above one. 
echo dirname(__FILE__) . "<br>"; 
?>

Output

C:\xampp\htdocs
C:\xampp\htdocs

4. __FUNCTION__

The __FUNCTION__ magic constant returns the name of the current function. If this is used outside the function, then it returns blank.

<?php
function FunctionTutorialsbook(){
echo "The function name is - " . __FUNCTION__;
}
FunctionTutorialsbook();
?>

Output

The function name is – FunctionTutorialsbook

5. __CLASS__

The __CLASS__ magic constant returns the name of the current class. Below is an example:

Example

<?php
class MagicClass
{
public function getClassName(){
return __CLASS__;
}
}
$obj = new MagicClass();
echo $obj->getClassName();
?>

Output

MagicClass

6. __METHOD__

The __METHOD__ magic constant returns the name of the current class method.

Example

<?php
class MagicClass
{
public function MagicMethod(){
return __METHOD__;
}
}
$obj = new MagicClass();
echo $obj->MagicMethod();
?>

Output

MagicClass::MagicMethod

7. __NAMESPACE__

The __NAMESPACE__ magic constant returns the name of the current namespace.

Example

<?php
namespace MagicNamespace;
class MyClass
{
public function getNamespace(){
return __NAMESPACE__;
}
}
$obj = new MyClass();
echo $obj->getNamespace(); 
?>

Output

MagicNamespace

Please get connected & share!

Advertisement