PHP | Magic Constants

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:

  • __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:



    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    The Line number is : 3
  • __file__: This magic constant return the full path of the executed file with the name of the file.

    Syntax:

    .__file__

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    The file name is : /home/3d27a639c57aaed9efa5880e613bc273.php
  • __dir__: This magic constant return the directory of the executed file.

    Syntax:

    .__dir__

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    The directory is : /home
  • __function__: This magic constant return the name of the function where this magic constant is included.

    Syntax:

    .__function__

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:


    The function name is : Geeks
  • __class__: This magic constant return the name of the class where this magic constant is included.

    Syntax:

    __class__

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

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

    Syntax:

    __method__

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    Company::GeeksforGeeks 
  • __namespace__: This magic constant return the current namepace where this magic constant is included.

    Syntax:

    __namespace__

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

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

    Syntax:


    __trait__

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

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

    Syntax:

    ClassName::class

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    Computer_Sciecnec_Portal\Geeks 

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.