Magic Constant

  • Magic constant is predefined constant of php.
  • Magic constant used for know some specific value like line number ,current  class  and function
  • Magic Constant-
    1. _FILE_
    2. _LINE_
    3. _DIR_
    4. _CLASS_
    5. _FUNCTION_
    6. _METHOD_
    7. PHP_VERSION

    .

_FILE_ :-

  • its return full path of current file directory
_FILE_ Example
<?php echo "file full path with file name is:- " . __FILE__ ; ?>

Result

file full path with file name is:- C:\xampp\htdocs\test1\test.php

_LINE_

  • Its return line number of current file
_LINE_ Example

<?php echo "line number is " . __LINE__ ; ?>

Result

 line number is 3

_DIR_

  • Its return directory of the file
_DIR_ Example

<?php echo "file full path is '" . __DIR__ ; ?>

Result

file full path is 'C:\xampp\htdocs\test1

_CLASS_

  • Its return current class name.
_CLASS_ Example

<?php 
class MagicConstant
 { 
      public function ClassNameTest()
       {
           echo "This is " . __CLASS__ . " class."; 
       } 
 } 
$classObject = new MagicConstant();
$classObject->ClassNameTest();

?>

Result

This is MagicConstant class

_METHOD_

  • Its return class method name.
_METHOD_ Example

<?php 
class MagicConstant 
{
 public function ShowMethodName() 
      { 
     echo "This is " . __METHOD__ . " method.";
      }
}
 $classObject = new MagicConstant();
 $classObject->ShowMethodName();

?>

Result

This is MagicConstant::ShowMethodName method.

_FUNCTION_

  • Its return function name.
_FUNCTION_ Example

<?php
   class MagicConstant 
{ 
        public function ShowFunctionName() 
             { 
                echo "This is function '" . __FUNCTION__ . "'."; 
             } 
}
 $classObject = new MagicConstant();
 $classObject->ShowFunctionName();

?>

Result

This is function 'ShowFunctionName'.

PHP_VERSION

  • its return current version of php

PHP_VERSION Example

<?php echo "Php version is: " . PHP_VERSION ; ?>

Result

Php version is:7.0.9

15 Replies to “Magic Constant”

  1. I just wanted to type a brief message to be able to express gratitude to you for all the nice pointers you are sharing on this website. My time intensive internet search has at the end of the day been paid with brilliant tips to talk about with my classmates and friends. I would tell you that many of us website visitors are rather blessed to live in a fine website with many perfect people with interesting tricks. I feel rather happy to have used your webpages and look forward to some more amazing times reading here. Thanks once more for a lot of things.

  2. I’m impressed, I have to admit. Genuinely rarely should i encounter a weblog that’s both educative and entertaining, and let me tell you, you may have hit the nail about the head. Your idea is outstanding; the problem is an element that insufficient persons are speaking intelligently about. I am delighted we came across this during my look for something with this.

Leave a Reply

Your email address will not be published. Required fields are marked *