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__
;
?>
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__
;
}
Geeks();
?>
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();
?>
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();
?>
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();
?>
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();
?>
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
?>
Computer_Sciecnec_PortalGeeks
Link: https://www.php.net/manual/en/language.constants.predefined.php