Wednesday, August 30, 2017

PHP Magic Constants


Magic constants are the predefined constants in PHP that get changed on the basis of their use. They start with double underscore (__) and ends with double underscore.

They are similar to other predefined constants but as they change their values with the context, they are called magic constants. Magic constants are case-insensitive.

There are eight magical constants defined in the below table.
NameDescription
__LINE__Represents current line number where it is used.
__FILE__Represents full path and file name of the file. If it is used inside an include, name of included file is returned.
__DIR__Represents full directory path of the file. Equivalent to dirname(__file__). It does not have a trailing slash unless it is a root directory. It also resolves symbolic link.
__FUNCTION__Represents the function name where it is used. If it is used outside of any function, then it will return blank.
__CLASS__Represents the function name where it is used. If it is used outside of any function, then it will return blank.
__TRAIT__Represents the trait name where it is used. If it is used outside of any function, then it will return blank. It includes namespace it was declared in.
__METHOD__Represents the name of the class method where it is used. The method name is returned as it was declared.
__NAMESPACE__Represents the name of the current namespace.

Let see the examples one by one.

__LINE__
<?php
echo "Line number " . __LINE__ . "<br>"; // Displays: Line number 2
echo "Line number " . __LINE__ . "<br>"; // Displays: Line number 3
?>
Output:
--------------------
Line number 2
Line number 3

__FILE__
<?php
// Displays the absolute path of this file
echo "The full path of this file is: " . __FILE__;
?>
Output:
------------------------
The full path of this file is: C:/localhost/demo/main.php

__DIR__
<?php
// Displays the directory of this file
echo "The directory of this file is: " . __DIR__;
?>
Output:
------------------
The directory of this file is: C:/localhost/demo/

__FUNCTION__
<?php
function mainFunction(){
    echo  "The function name is - " . __FUNCTION__;
}
mainFunction(); // Displays: The function name is - myFunction
?>
Output:
---------------
The function name is - mainFunction

__CLASS__
<?php
class DemoClass
{
    public function getClassName(){
        return __CLASS__;
    }
}
$obj = new DemoClass();
echo $obj->getClassName(); // Displays: MyClass
?>
Output:
----------------
DemoClass

__METHOD__
<?php
class DemoClass
{
    public function mainMethod(){
        echo __METHOD__;
    }
}
$obj = new DemoClass();
$obj->mainMethod(); // Displays: Sample::myMethod
?>
Output:
-------------------
DemoClass::mainMethod

__NAMESPACE__
<?php
namespace PhpNamespace;
class DemoClass
{
    public function getNamespace(){
        return __NAMESPACE__;
    }
}
$obj = new DemoClass();
echo $obj->getNamespace(); // Displays: MyNamespace
?>
Output:
-----------------------
PhpNamespace




No comments:

Post a Comment