Open In App

PHP | Magic Constants

Last Updated : 17 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Magic constants: Magic constants are the predefined constants in PHP which is used on the basis of their use. These constants are created by various extensions. There are nine magic constant in the PHP and all of the constant resolved at the compile-time, not like the regular constant which is resolved at run time. There are eight magic constants that start and end with double underscores (__). 
All the constants are listed below with the example code: 

1. __line__: This magic constant return the current line number of the file. If you use this magic constant in your program file somewhere then this constant will display the line number during compile time.

Syntax: 

.__line__

Example: 

PHP




<?php
  
echo "The Line number is : ". __line__;
  
?>


Output: 

The Line number is : 3

2. __file__: This magic constant return the full path of the executed file with the name of the file.

Syntax: 

.__file__

Example: 

PHP




<?php
  
echo "The file name is : ". __file__;
  
?>


Output: 

The file name is : /home/3d27a639c57aaed9efa5880e613bc273.php

3. __dir__: This magic constant return the directory of the executed file.

Syntax: 

.__dir__

Example: 

PHP




<?php
  
echo "The directory is : ". __dir__;
  
?>


Output: 

The directory is : /home

4. __function__: This magic constant return the name of the function where this magic constant is included.

Syntax: 

.__function__

Example: 

PHP




<?php
function Geeks(){
    echo "The function name is : ". __function__;
}
Geeks();
?>


Output: 

The function name is : Geeks

5. __class__: This magic constant return the name of the class where this magic constant is included.

Syntax: 

__class__

Example: 

PHP




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


Output: 

Geeks 

6. __method__: This magic constant return the method name where this magic constant is included.

Syntax: 

__method__

Example: 

PHP




<?php
class Company
{
    public function GeeksforGeeks(){
        return __method__;
    }
}
$obj = new Company();
echo  $obj->GeeksforGeeks();
?>


Output: 

Company::GeeksforGeeks 

7. __namespace__: This magic constant return the current namespace where this magic constant is included.

Syntax: 

__namespace__

Example: 

PHP




<?php
namespace GeeksforGeeks;
 
class Company {
    public function gfg() {
        return __namespace__;
    }
}
 
$obj = new Company();
echo  $obj->gfg();
 
?>


Output: 

GeeksforGeeks

8. __trait__: This magic constant return the trait name where this magic constant is included.

Syntax: 

__trait__

Example: 

PHP




<?php
trait GeeksforGeeks{ 
    function gfg(){ 
        echo __trait__; 
        
    
    class Company{ 
        use GeeksforGeeks; 
        
    $a = new Company; 
    $a->gfg(); 
?>


Output: 

GeeksforGeeks 

9. ClassName::class: This magic constant return the fully qualified class name.

Syntax: 

ClassName::class

Example: 

PHP




<?php
 
namespace Computer_Sciecnec_Portal;
class Geeks{ }
 
echo Geeks::class;//Classname::class
 
?>


Output: 

Computer_Sciecnec_Portal\Geeks 

Reference: https://www.php.net/manual/en/language.constants.predefined.php
 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads