+

PHP Magic Constants

Magic constants:Magic constants - these are predefined constants in PHP that are used based on their usage. These constants are created by various extensions. There are nine magic constants in PHP, and all constants are resolved at compile time, as opposed to a regular constant that is resolved at runtime. There are eight magic constants that start and end with double underscores (__). 
All constants are listed below with sample code:
  • __line__:this magic constant returns the current line number of a file. If you use this magic constant in your program file, then this constant will display the line number at compile time. Syntax:
    .__line__
    Example : echo "The Line number is:" . __ line__ ;  
    ?>
    Output:
    The Line number is: 3 
  • __file__:this magic constant returns the full path to the executable with filename. Syntax:
    .__ file__ 
    Example : < ? php echo "The file name is:" . __ file__ ;  
    ?>
    Output:
    The file name is: /home/3d27a639c57aaed9efa5880e613bc273.php 
  • __dir__:this magic constant returns the directory of the executable. Syntax:
    .__ dir__ 
    Example : < ? php echo "The directory is:" . __dir__;  
    ?>
    Output:
    The directory is: / home 
  • __function__:this magic constant returns the name of the function that contains this magic constant. Syntax:
    .__ function__ 
    Example : function Geeks() { echo "The function name is:" . __ function__ ; }
    Geeks();
    ?>
    Output:
    The function name is: Geeks 
  • __class__:this magic constant returns the name of the class that this magic constant belongs to . Syntax:
    __class__
    Example : class Geeks { public function getClassName() { return __ class__ ; } } $obj = new Geeks(); echo $obj -> getClassName(); ?> Output:
    Geeks 
  • __method__:this magic constant returns the name of the method that this magic constant is included in. Syntax:
    __method__ 
    Example : class Company { public function GeeksforGeeks() { return __ method__ ; } } $obj = new Company(); echo  $obj -> GeeksforGeeks(); ?> Output:
    Company::GeeksforGeeks 
  • __namespace__:this magic constant returns the current namespace this magic constant belongs to. Syntax:
    __namespace__ 
    Example : namespace GeeksforGeeks; class Company { public function gfg() { return __ namespace__; } } $obj = new Company(); echo  $obj -> gfg();  
    ?>
    Output:
    GeeksforGeeks 
  • __trait__:this magic constant returns the trait name where this magic constant is enabled. Syntax:
    __trait__
    Example : trait GeeksforGeeks { function gfg() { echo __ trait__; } } class Company { use GeeksforGeeks; } $a = new Company; $a -> gfg(); ?> Output:
    GeeksforGeeks 
  • ClassName::class:this magic constant returns the fully qualified class name. Syntax:
    ClassName::class 
    Example : namespace Computer_Sciecnec_Portal ; class Geeks {} echo Geeks :: class ; // Classname::class 
    ?>
    Output:
    Computer_Sciecnec_PortalGeeks 
Link: https://www.php.net/manual/en/language.constants.predefined.php
Get Solution for free from DataCamp guru