abc

Share This blog with your friends, so that we can improve more & more . our aim is to easy & simple way of learning.

12/15/2020

What are magic constant in PHP ?

 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.


An Introduction to the Laravel Framework: What It Is and Why You Should Use It

  If you're a PHP developer looking for a modern, efficient, and powerful framework to build web applications, look no further than Lara...