Magic constants are the predefined constants in PHP which is changed depending on where they are used. They start with double underscore (__) and ends with double underscore.
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 and the regular constant which is resolved at run time.
PHP's magic constants
All of the constants are resolved at compile-time instead of run time unlike the regular constant. Magic constants are case-insensitive.
List of PHP's magic constants
Name | Description |
__LINE__ | It returns the current line number of the file |
__FILE__ | It returns the full path and filename of the executed file. If it is used inside the include, the name of the included file is returned. |
__DIR__ | It returns the directory of the file. If it is used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__). This directory name does not have a trailing slash unless it is the root directory. |
__FUNCTION__ | It returns the function name where this constant is used. It will return blank if it is used outside of any function. |
__CLASS__ | It returns the class name. The class name includes the namespace if it was declared in (e.g. Pack\Cla ). When used in a trait method, __CLASS__ is the name of the class the trait is used in. |
__TRAIT__ | It returns the trait name. The trait name includes the namespace if it was declared in (e.g. Pack\Cla ). |
__METHOD__ | It returns the class method name. |
__NAMESPACE__ | It returns the name of the current namespace. |
ClassName::class | It does not start and end with the double underscore (__). It returns the fully qualified name of the ClassName. It is useful with namespaced classes. |
PHP Code Example for __LINE__:
<?php
echo "Printing at line number " . __LINE__ . "<br>";
?>
Output:
Printing at line number 2
PHP Code Example for __FILE__:
<?php
echo "Print full path of file : ".__FILE__ . "<br>";
?>
Output:
Print full path of file : E:\xampp\htdocs\phptraining\magic_constant.php
PHP Code Example for __DIR__:
<?php
echo "Print full path of directory : ".__DIR__ . "<br>";
?>
Output:
Print full path of directory : E:\xampp\htdocs\phptraining
PHP Code Example for __FUNCTION__:
<?php
//Using magic constant inside function.
function get_func(){
echo 'The function name is '. __FUNCTION__ . "<br>";
}
get_func();
?>
Output:
The function name is get_func
PHP Code Example for __CLASS__:
<?php
//Get Class name using magic constant __CLASS__
class ARYATECHNO
{
public function __construct() { }
function getClassName(){
echo "Class name is ".__CLASS__ . "<br>";
}
}
$obj = new ARYATECHNO;
$obj->getClassName();
?>
Output:
Class name is ARYATECHNO
PHP Code Example for __TRAIT__:
<?php
//prints name of the trait
trait get_Trait {
function print_trait(){
echo "Name of the trait :".__TRAIT__. "<br>";
}
}
class ARYATECH {
use get_Trait;
}
$obj = new ARYATECH;
$obj->print_trait();
?>
Output:
Name of the trait :get_Trait
PHP Code Example for __METHOD__:
<?php
//Get method name using magic constants
class MAGIC {
public function __construct() {
echo "Method name is ".__METHOD__ . "<br>";
}
public function get_method(){
echo "Method name is ".__METHOD__."<br>";
}
}
$obj = new MAGIC;
$obj->get_method();
?>
Output:
Method name is MAGIC::__construct
Method name is MAGIC::get_method
PHP Code Example for __NAMESPACE__:
<?php
namespace PHPCODE;
//Get namespace name using magic constants
class package {
public function __construct() {
echo 'Namespace name is '.__NAMESPACE__."<br>";
}
}
$obj = new package;
?>
Output:
Namespace name is PHPCODE
PHP Code Example for CLASSNAME::CLASS:
<?php
class CLASSA{
}
echo "Get class name : ".CLASSA::class."<br>";
?>
Output:
Get class name : PHPCODE\CLASSA
Comments