PHP Magic (Predefined) Constants (With Examples)

By Shahnail Khan Last updated : December 7, 2023

PHP Magic Constants

PHP includes 9 predefined constants which are also called PHP magic constants that serve various purposes.

All magic constants (except ClassName::class constant) are written with double underscores (__) at both of the start and the end.

The following are the magic (predefined) constants in PHP:

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

Let's have a look at the nine magical PHP constants with examples of each.

1) PHP __LINE__ Constant

The __LINE__ constant represents the current line number of the file.

Example of __LINE__ Constant

<?php
echo "This is line " . __LINE__ . " in the file.";
?>

The output of the above example is:

This is line 2 in the file.

In this example, we can see that echo statement displays the output in line 2.

2) PHP __FILE__ Constant

The __FILE__ constant represents the full path and filename of the file.

Example of __FILE__ Constant

<?php
echo "This file is " . __FILE__;
?>

The output of the above example is:

This file is /home/rZ0BiI/prog.php

In this code, the file path is displayed using __FILE__ constant.

3) PHP __DIR__ Constant

The __DIR__ constant represents the directory of the file.

Example of __DIR__ Constant

<?php
echo "This file is in the directory " . __DIR__;
?>

The output of the above example is:

This file is in the directory /home/LaWtdi

4) PHP __FUNCTION__ Constant

The __FUNCTION__ constant represents the name of the current function.

Example of __FUNCTION__ Constant

<?php
function example()
{
    echo "Function name is " . __FUNCTION__;
}
example();
?>

The output of the above example is:

Function name is example

In this code, we defined one function, namely "example". The echo statement displayed the function name using __FUNCTION__ constant.

5) PHP __CLASS__ Constant

The __CLASS__ constant represents the name of the current class.

Example of __CLASS__ Constant

<?php
class Includehelp
{
    public function displayClassName()
    {
        echo "Class name is " . __CLASS__;
    }
}

$obj = new Includehelp();
$obj->displayClassName();
?>

The output of the above example is:

Class name is Includehelp

6) PHP __METHOD__ Constant

The __METHOD__ constant represents the name of the current method.

Example of __METHOD__ Constant

<?php
class MyClass
{
    public function displayMethodName()
    {
        echo "Method name is " . __METHOD__;
    }
}

$obj = new MyClass();
$obj->displayMethodName();
?>

The output of the above example is:

Method name is MyClass::displayMethodName

In this code, the echo statement displays the method name using __METHOD__ constant. The method name is displayed as "Class name:: Method name".

7) PHP __NAMESPACE__ Constant

The __NAMESPACE__ constant represents the current namespace.

Example of __NAMESPACE__ Constant

<?php
namespace Includehelp;

function MagicalConst()
{
    return __NAMESPACE__;
}
?>
 
<!DOCTYPE html>
<html>
<body>

<?php echo "Current Namespace is : " . MagicalConst(); ?>

</body>
</html>

The output of the above example is:

Current Namespace is : Includehelp

In this code, we have displayed the current namespace, namely "Includehelp". The function must be created at the beginning inside the <?php tag. For displaying the namespace, the echo statement must be written inside the <body> tag.

8) PHP __TRAIT__ Constant

The __TRAIT__ constant represents the name of the current trait.

Example of __TRAIT__ Constant

<?php
trait MyTrait
{
    public function displayTraitName()
    {
        echo "Trait name is " . __TRAIT__;
    }
}

class MyClass
{
    use MyTrait;
}

$obj = new MyClass();
$obj->displayTraitName();
?>

The output of the above example is:

Trait name is MyTrait

In this code, the trait name, namely MyTrait is displayed using __TRAIT__ constant.

9) PHP ClassName::class Constant / Expression

The ClassName::class expression is used to get the name of a class, along with its namespace, if it has one.

Example of ClassName::class Constant

<?php
namespace Includehelp;

class PHP
{
    public function myValue()
    {
        return PHP::class;
    }
}
?>

<!DOCTYPE html>
<html>
<body>

<?php
$constants = new PHP();
echo $constants->myValue();
?>

</body>
</html>

The output of the above example is:

Includehelp\PHP

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.