1) __LINE__ it returns current line number
example
<?php
echo 'current line'.__LINE__;
?>
2) __FILE__ - get full path of current file
example - echo __FILE__ . "<br><br>";
3) __DIR__ - get full directory path
echo __DIR__ . "<br><br>";
OR
echo dirname(__FILE__) . "<br><br>";
4) __FUNCTION__ - get function name where magic constant used.
<?php
echo "<h3>Example for __FUNCTION__</h3>";
function test(){
echo 'The function name is '. __FUNCTION__ . "<br><br>";
}
test();
echo __FUNCTION__ . "<br><br>";
?>
5) __CLASS__ - get class name
below example output is 'base'
<?php
class base
{
function test_first(){
echo __CLASS__;
}
}
class child extends base
{
public function __construct() {
;
}
}
$t = new child;
$t->test_first();
?>
6) __TRAIT__ get trait name
for eg -
trait created_trait {
function jtp(){
echo __TRAIT__;
}
}
7) __METHOD__ get method name
public function meth_fun(){
//print method::meth_fun
echo __METHOD__;
}
8) __NAMESPACE__ current namespace
class name {
public function __construct() {
echo 'This line will print on calling namespace.';
}
}
$class_name = __NAMESPACE__ . '\name';
$a = new class_name;
9) ClassName::class - full name of class
output of below example is Technical_Portal/javatpoint
namespace Technical_Portal;
echo "<h3>Example for CLASSNAME::CLASS </h3>";
class javatpoint {
}
echo javatpoint::class;
in short magic contstant in php returns the current elements like filename, filepath, classname, methodname, directory path.