Magic constants in PHP – Learn in details with the examples

Spread the love

What are Magic constants in PHP?

The Magic constants in PHP are predefined constants that start and end with double underscores (__), with capital letters, and whose values change as per their use in a program.

Magic constants in PHP

These constants are called magic constants as their value changes where they will be used.
There are nine Magic constants in PHP. Let’s see them one by one.

Let’s see all the Magic Constants in PHP with example

1. __LINE__

The __LINE__ magic constant will give you the current line number where it is used in a program.

Example:

<?php
 echo "This is line no ".__LINE__;
 echo "<br>This is line no ".__LINE__;
 echo "<br>This is line no ".__LINE__;
?>

Output:

This is line no 2
This is line no 3
This is line no 4

 

2. __FILE__

The __FILE__ magic constant will give the full path and the current file name where it is used.

<?php
 echo "<h3>__FILE__ Example:</h3>";
 
 echo "The path of this file is = ".__FILE__;
?>


Output:

__FILE__ Example:
The path of this file is = C:xampphtdocsphpclassoopmagicconstantsfile.php

 

3. __DIR__

The __DIR__ magic constant will give you a full directory path the file where it is used. This constant will not show trailing slash in the path unless it is a root directory of a file.

Example:

<?php
 echo "<h3>__DIR__ Example:</h3>";
 
 echo "The path of this file is = ".__DIR__;
?>


Output:

__DIR__ Example:
The path of this file is = C:xampphtdocsphpclassoopmagicconstants

 

4. __FUNCTION__

The __FUNCTION__  will give you the function name where it is used. It will work only inside the function, outside the function, it will return a blank value.

Example:

<?php
 echo "<h3>__FUNCTION__ Example:</h3>";
 
 function showName()
 {
  //__FUNCTION__ inside a function
  echo "The name of this function is ".__FUNCTION__;
 }
 
 showName();
 
 //__FUNCTION__ outside a function
 echo "<br>The name of this functio is ".__FUNCTION__;
 
?>


Output:

__FUNCTION__ Example:
The name of this function is showName
The name of this functio is

 

5. __CLASS__

The __CLASS__ magic constant will you the name of a current class where it is used. It is one of the important Magic constants in PHP. This is one of the most used Magic constants in PHP.

Example:

<?php
 echo "<h3>__CLASS__ Example:</h3>";
 
 class Test
 {
  function showName()
  {
   echo "The name of a class is = ".__CLASS__;
  }
 }
 
 $test = new Test;
 $test->showName();
?>


Output:

__CLASS__ Example:
The name of a class is = Test

 

6. __TRAIT__

The __TRAIT__ magic constant will give you the current trait name where it is used.

<?php
 echo "<h3>__TRAIT__ Example:</h3>";
 
 Trait testTrait
 {
  function showName()
  {
   echo "The name of a trait is = ".__TRAIT__;
  }
 }
 class Test
 {
  use testTrait;
 }
 
 $test = new Test;
 $test->showName();
?>


Output:

__TRAIT__ Example:
The name of a trait is = testTrait

 

7. __METHOD__

__METHOD__ is another one in Magic constants in PHP. The __METHOD__ constant will return the name of a class method where it is used. It will also show the name of a class with a scope resolution operator.

Example:

<?php
 echo "<h3>__METHOD__  Example:</h3>";
 
 class Test
 {
  function showName()
  {
   echo "The name of a method is = ".__METHOD__;
  }
 }
 
 $test = new Test;
 $test->showName();
?>


Output:

__METHOD__  Example:
The name of a method is = Test::showName

 

8. __NAMESPACE__

The __NAMESPACE__ constant will return the current namespace of a file where it is used.

Example:

<?php
 namespace TestNamespace;
 echo "<h3>__NAMESPACE__  Example:</h3>";
 
 class Test
 {
  function showName()
  {
   echo "The name of a method is = ".__NAMESPACE__;
  }
 }
 
 $test = new Test;
 $test->showName();
?>


Output:

__NAMESPACE__  Example:
The name of a method is = TestNamespace

 

9. ClassName::class

The ClassName::class will you the fully qualified name of a class. It is particularly useful with the namespaced classes. Here “ClassName” can be any class with which you want to use it.

Example:

<?php
 echo "<h3>ClassName::class Example:</h3>";
 
 namespace TestNamespace 
 {
  class ClassTest
  {
  
  }
    
  echo ClassTest::class;
 }
?>


Output:

TestNamespaceClassTest

In this tutorial, you have seen the Magic constants in PHP with examples.

In the next tutorial, you will learn about PHP Anonymous functions in detail.


Spread the love

Leave a Comment