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:
<?phpecho"The Line number is : ".__line__;?>chevron_rightfilter_noneOutput:
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:
<?phpecho"The file name is : ".__file__;?>chevron_rightfilter_noneOutput:
The file name is : /home/3d27a639c57aaed9efa5880e613bc273.php
- __dir__: This magic constant return the directory of the executed file.
Syntax:
.__dir__
Example:
<?phpecho"The directory is : ". __dir__;?>chevron_rightfilter_noneOutput:
The directory is : /home
- __function__: This magic constant return the name of the function where this magic constant is included.
Syntax:
.__function__
Example:
<?phpfunctionGeeks(){echo"The function name is : ".__function__;}Geeks();?>chevron_rightfilter_noneOutput:
The function name is : Geeks
- __class__: This magic constant return the name of the class where this magic constant is included.
Syntax:
__class__
Example:
<?phpclassGeeks{publicfunctiongetClassName(){return__class__;}}$obj=newGeeks();echo$obj->getClassName();?>chevron_rightfilter_noneOutput:
Geeks
- __method__: This magic constant return the method name where this magic constant is included.
Syntax:
__method__
Example:
<?phpclassCompany{publicfunctionGeeksforGeeks(){return__method__;}}$obj=newCompany();echo$obj->GeeksforGeeks();?>chevron_rightfilter_noneOutput:
Company::GeeksforGeeks
- __namespace__: This magic constant return the current namepace where this magic constant is included.
Syntax:
__namespace__
Example:
<?phpnamespaceGeeksforGeeks;classCompany {publicfunctiongfg() {return__namespace__;}}$obj=newCompany();echo$obj->gfg();?>chevron_rightfilter_noneOutput:
GeeksforGeeks
- __trait__: This magic constant return the trait name where this magic constant is included.
Syntax:
__trait__
Example:
<?phptraitGeeksforGeeks{functiongfg(){echo__trait__;}}classCompany{useGeeksforGeeks;}$a=newCompany;$a->gfg();?>chevron_rightfilter_noneOutput:
GeeksforGeeks
- ClassName::class: This magic constant return the fully qualified class name.
Syntax:
ClassName::class
Example:
<?phpnamespaceComputer_Sciecnec_Portal;classGeeks{ }echoGeeks::class;//Classname::class?>chevron_rightfilter_noneOutput:
Computer_Sciecnec_Portal\Geeks
Reference: https://www.php.net/manual/en/language.constants.predefined.php
Recommended Posts:
- PHP | Constants
- PHP | Defining Constants
- p5.js | Constants | QUARTER_PI
- p5.js | Constants | TAU
- p5.js | Constants | HALF_PI
- p5.js | Constants | PI
- p5.js | Constants | TWO_PI
- Node.js | os.constants
- Node.js | zlib.constants Property
- PHP 5 vs PHP 7
- PHP | Get PHP configuration information using phpinfo()
- PHP | php.ini File Configuration
- How to import config.php file in a PHP script ?
- LAMP installation and important PHP configurations on Ubuntu
- PHP | imagecreatetruecolor() Function
- Maximum execution time taken by a PHP Script
- PHP program to fetch data from localhost server database using XAMPP
- PHP | ImagickDraw getTextAlignment() Function
- PHP | Ds\Sequence last() Function
- PHP | Imagick floodFillPaintImage() Function
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.

