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

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () 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