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:
    Example : echo "The Line number is:" . __ line__ ;  
    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__ ;  
    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__;  
    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__ ; }
    The function name is: Geeks 
  • __class__:this magic constant returns the name of the class that this magic constant belongs to . Syntax:
    Example : class Geeks { public function getClassName() { return __ class__ ; } } $obj = new Geeks(); echo $obj -> getClassName(); ?> Output:
  • __method__:this magic constant returns the name of the method that this magic constant is included in. Syntax:
    Example : class Company { public function GeeksforGeeks() { return __ method__ ; } } $obj = new Company(); echo  $obj -> GeeksforGeeks(); ?> Output:
  • __namespace__:this magic constant returns the current namespace this magic constant belongs to. Syntax:
    Example : namespace GeeksforGeeks; class Company { public function gfg() { return __ namespace__; } } $obj = new Company(); echo  $obj -> gfg();  
  • __trait__:this magic constant returns the trait name where this magic constant is enabled. Syntax:
    Example : trait GeeksforGeeks { function gfg() { echo __ trait__; } } class Company { use GeeksforGeeks; } $a = new Company; $a -> gfg(); ?> Output:
  • ClassName::class:this magic constant returns the fully qualified class name. Syntax:
    Example : namespace Computer_Sciecnec_Portal ; class Geeks {} echo Geeks :: class ; // Classname::class 
Link: https://www.php.net/manual/en/language.constants.predefined.php
Get Solution for free from DataCamp guru