Change language

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 


Best laptop for Sims 4


Best laptop for Zoom


Best laptop for Minecraft


Best laptop for engineering student


Best laptop for development


Best laptop for Cricut Maker


Best laptop for hacking


Best laptop for Machine Learning


Latest questions


psycopg2: insert multiple rows with one query

12 answers


How to convert Nonetype to int or string?

12 answers


How to specify multiple return types using type-hints

12 answers


Javascript Error: IPython is not defined in JupyterLab

12 answers


Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method