Magic Constant

Magic Constants

Magic constants are the predefined constants in PHP which 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. There are nine magic constants in PHP. In which eight magic constants start and end with double underscores (__).

  • __LINE__
  • __FILE__
  • __DIR__
  • __FUNCTION__
  • __CLASS__
  • __TRAIT__
  • __METHOD__
  • __NAMESPACE__
  • ClassName::class

All of the constants are resolved at compile-time instead of run time, unlike the regular constant. Magic constants are case-insensitive.

Changelog

There is one more operator called conditional operator. This first evaluates an expression for a true or false value and then execute one of the two given statements depending upon the result of the evaluation. The conditional operator has this syntax

Version Description
5.3.0Added __DIR__ and __NAMESPACE__ magic constant
5.4.0Added __TRAIT__ magic constant
5.5.0Added ::class magic constant

__LINE__

It returns the current line number of the file, where this constant is used.

In the following example $x is an integer. The PHP var_dump() function returns the data type and value:

  •       
                            <?php   
    echo "

    Example for __LINE__

    "; // print Your current line number i.e;4 echo "You are at line number " . __LINE__ . "

    "; ?>

    __FILE__

    This magic constant returns the full path of the executed file, where the file is stored. If it is used inside the include, the name of the included file is returned.

    Variable with global scope:

  •       
                            <?php   
    echo "

    Example for __FILE__

    "; //print full path of file with .php extension echo __FILE__ . "

    "; ?>

    __DIR__:

    It returns the full directory path of the executed file. The path returned by this magic constant is equivalent to dirname(__FILE__). This magic constant does not have a trailing slash unless it is a root directory.

  •       
                            <?php   
    echo "

    Example for __DIR__

    "; //print full path of directory where script will be placed echo __DIR__ . "

    "; //below output will equivalent to above one. echo dirname(__FILE__) . "

    "; ?>

    __FUNCTION__:

    This magic constant returns the function name, where this constant is used. It will return blank if it is used outside of any function.

  •       
                            <?php   
    echo "

    Example for __FUNCTION__

    "; //Using magic constant inside function. function test(){ //print the function name i.e; test. echo 'The function name is '. __FUNCTION__ . "

    "; } test(); //Magic constant used outside function gives the blank output. function test_function(){ echo 'Hie'; } test_function(); //give the blank output. echo __FUNCTION__ . "

    "; ?>

    __CLASS__:

    It returns the class name, where this magic constant is used. __CLASS__ constant also works in traits.

  •       
                            <?php   
    echo "

    Example for __CLASS__

    "; class JTP { public function __construct() { ; } function getClassName(){ //print name of the class JTP. echo __CLASS__ . "

    "; } } $t = new JTP; $t->getClassName(); //in case of multiple classes class base { function test_first(){ //will always print parent class which is base here. echo __CLASS__; } } class child extends base { public function __construct() { ; } } $t = new child; $t->test_first(); ?>

    __TRAIT__

    This magic constant returns the trait name, where it is used.

  •       
                            <?php   
    echo "

    Example for __TRAIT__

    "; trait created_trait { function jtp(){ //will print name of the trait i.e; created_trait echo __TRAIT__; } } class Company { use created_trait; } $a = new Company; $a->jtp(); ?>

    __METHOD__:

    It returns the name of the class method where this magic constant is included. The method name is returned the same as it was declared.

  •       
                             
    
    <?php   
        echo "

    Example for __METHOD__

    "; class method { public function __construct() { //print method::__construct echo __METHOD__ . "

    "; } public function meth_fun(){ //print method::meth_fun echo __METHOD__; } } $a = new method; $a->meth_fun(); ?>

    __NAMESPACE__:

    It returns the current namespace where it is used.

  •       
                            <?php  
    echo "

    Example for __NAMESPACE__

    "; class name { public function __construct() { echo 'This line will print on calling namespace.'; } } $class_name = __NAMESPACE__ . '\name'; $a = new class_name; ?>

    ClassName::class

    This magic constant does not start and end with the double underscore (__). It returns the fully qualified name of the ClassName. ClassName::class is added in PHP 5.5.0. It is useful with namespaced classes.

  •       
                            <?php  
    namespace Technical_Portal;  
    echo "

    Example for CLASSNAME::CLASS

    "; class javatpoint { } echo javatpoint::class; //ClassName::class ?>

    Dess App

    DessApp is an Integrated E-learning Education, Interactive and User-friendly features, smarter options and redefining your school costs effectively and efficiently.

    View
    1 1