Back to Top

How to define PHP Magic constants

PHP provides a number of predefined constants.PHP constants with the two underscores (__) are called the magic methods, which are not actually constants but has the major role in PHP.The magic constants are mostly used to get the information about the current environment.If some things are not often used, it is easy to forget that but using magic constant, you can make some special features in PHP very easily.

Constant, name itself says it is a constant means can’t change it is a value and the magic constant name says it is a specific constant to create magic in PHP.PHP offers some predefined constants which can we use in many PHP scripts.A good PHP developer always develops clean and easily understandable code and you can make this thing possible with the help of Magical Constants.

There are eight magic constants provided by PHP like __LINE__, __FILE__ etc

NOTE: These Magical constants are case-insensitive

SYNTAX:

  
2 underscore (__) before and after the name

Let’s see about PHP Magic Constants one by one,

1. __FILE__

Display the full path and filename of the file.It is very useful to find the path of your file.If used within the included file, It returns the included file name.__ FILE__ contains an absolute path of the file.

The dirname (__ FILE__) function returns the directory path to the file in PHP.

echo __FILE__;
// Prints D:xamp/php/docs/index.php 

2. __LINE__

Display current line number of the file.It is very helpful constant for debugging purpose.With this, you can easily get details about the line numbers in files.

echo __LINE__;
 // Prints 2 

3. __DIR__

Display current directory of the file.Its really very helpful PHP magic constant because its give directory path or can say absolute path so we can easily sort out the path of directories. It is equivalent to dirname (__ FILE__) function.

echo __DIR__;
 // Prints D:xampphtdocs  

4. __FUNCTION__

Display function name and added in PHP 4.3.0. It return the name of Function means when the function was defined is and returned result would be case-sensitive. In PHP 4, it values is lowercase.

Let’s take an idea about this Magic constant.

 
function fun_name()
 {
     echo __FUNCTION__;
 }
fun_name();
// Prints fun_name

5. __CLASS__

Display the class name in a case-sensitive manner.It’s one of the interesting Magic constant because it returns class name as a result.

class testClass
{
   function __construct() {
           echo __CLASS__;
   }
}

$obj = new testClass();
// Prints testClass

Here are the few other articles which you might like

Static Keyword in PHP
Type Hinting in PHP5
To Reset the WordPress Loop

6. __METHOD__

Display the method name of the class.One of my favorite and powerful Magic constant because its returns declared method name of the class. so as PHP developer, it is not a good constant for us?

class testClass
{
      function fun_test() {
              echo __METHOD__;
      }
}

$obj = new testClass();
$obj->fun_test();
 // Prints testClass::fun_test 

7. __NAMESPACE__

Display the name of the current namespace and its newly added feature in PHP 5.3

namespace test;

echo __NAMESPACE__;
// Prints test

8. __TRAIT__

Last but not the least a TRAIT a new magical constant.it will display the trait name and it is added in new PHP in PHP 5.4

class A {
    public function funcA() {
          }
}

trait Test_trait {
    public function funcA() {
        parent::funcA();
        echo __TRAIT__;
    }
}

class B extends A {
    use Test_trait;
}

$obj = new B();
$obj->funcA();
 // Prints Test_trait

NOTE: PHP 5.4 is not stable yet, So you can visit this link to run the TRAIT code in PHP 5.4

Overall try to use magic constants in your PHP application.Do let us know if you face any issues while using magic constants in PHP application.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

An Introduction to Gulp JS

Posted on 2 years ago

Bhumi

WordPress Template Hierarchy Tutorial

Posted on 3 years ago

Bhumi

How to use MySQL Cursor

Posted on 7 years ago

Bhumi

How to use Ajax in WordPress

Posted on 6 years ago

Bhumi

Hello world

Updated 1 month ago

Nick Carter

In depth Understanding of Laravel Core

Updated 3 months ago

Bhumi

ECMAScript 6 New Features – 2

Updated 1 year ago

Bhumi

The Reader’s Poll – June 2015

Posted on 4 years ago

Bhumi

The Reader’s Poll – August 2014

Posted on 5 years ago

Bhumi

The Reader’s Poll – June 2014

Posted on 5 years ago

Bhumi

The Readers’ Poll – May 2014

Posted on 5 years ago

Bhumi

The Readers’ Poll – August 2013

Posted on 6 years ago

Bhumi