Skip to main content

Magic Constants in PHP Programming

Like magic functions there are pre-defined constants which are know as Magic constants in PHP. In this tutorial we will explore what is magic constant, how they are different from normal constants, how to use them. To understand/read this topic I am assuming that you have basic understanding of PHP and have idea about constants in php.

Magic Constants in PHP

What are the Magic Constants in PHP

Magic constants are the pre-defined constants in PHP which value get changed on the basis where they are used. Magic constants in PHP starts with double underscore and also end with double underscore for exaple __FILE__.

Magic constants are like other pre-defined constants in PHP but since its value get change as per the context they are called Magic constants. For example, if we will echo __FILE then-then it will print the name of the script file in which we have used.

Following are the magic constants in PHP

  1. __LINE__
  2. __FILE__
  3. __DIR__
  4. __FUNCTION__
  5. __CLASS__
  6. __TRAIT__
  7. __METHOD__
  8. __NAMESPACE__

From their names they are very much self explanatory. For example __LINE__ will give the current code line number where it is used, __CLASS__ will give the name of class. So let us has quick glance of where they are commonly used in real programming to solve our problem.

__LINE__ Magic Constant in PHP

__LINE__ will give the current line number where it is used. This is one of vary rarely used magic constant. Sometime it is used to debug or add line number in PHP script. Following is an example of __LINE__

<?php
echo "You are at line number " . __LINE__;// print You are at line number 2

But following will have different output

<?php

echo “You are at line number “. __LINE__;// print You are at line number 3


<?php
//This is comment line 1
//This is comment line 2
echo "You are at line number " . __LINE__;// print You are at line number 4

__FILE__ Magic Constant in PHP

__FILE__ will give full path of filename in which it is used. It will also resolve the symbolic link. Before PHP 4.0.2 it returns relative file path also. __FILE__ lists in one of the most used magic constants. Magic constan __FILE__ mostly used to file real directory path where script resides. Most of the framework uses __FILE__.

Below are some example of __FILE__
<?php
echo __FILE__;//Will print full path of file with .php extension

People commonly use it to get the directory path before PHP 5.3.0
<?php
echo dirname(__FILE__); //Will print the full path of directory where file resides

__DIR__ Magic Constant in PHP

__DIR__ magic constant will give full absolute path of directory where your script file resides. it is nothing but a replacement of method dirname(__FILE__). __DIR__ is added from PHP 5.3.0 . It also resolve the symbolic link.
Below is the example
<?php
echo __DIR__;//Full path of directory where script will be placed

__FUNCTION__ Magic Constant in PHP

__FUNCTION__ will give name of the function where it is used. __FUNCTION__ magic method is introduced from PHP version 4.3.0.

function test_function(){
echo 'the function name is '. __FUNCTION__;//the function name is test_function
}
test_function();

But if you will __FUNCTION__ outside of any function then it will give blank.

<?php
function test_function(){
echo 'the function name is '. __FUNCTION__;//the function name is test_function
}
test_function();

echo __FUNCTION__;//Nothing will be printed

Also it will print the name of function in class
class test
{
public function __construct() {
;
}
function test_func(){
echo __FUNCTION__;//will print test_func
}
}
$t = new test;
$t->test_func();

What’s about magic methods? It will print __call instead of the name of the function called.

class test
{
public function __construct() {
;
}
function __call($name , $ars){
echo __FUNCTION__; //It will print __call
}
}
$t = new test;
$t->psudo_method();

__CLASS__ Magic Constant in PHP

__CLASS__ magic method will give the name of the class where it is used. Like other Magic constants in PHP if it will be used outside the class then it will give blank string. This constant is available from PHP 4.3.0

class test
{
public function __construct() {
;
}
function test_method(){
echo __CLASS__; //It will print test
}
}
$t = new test;
$t->test_method();

If it will used with inheritence then it will still only print the name of the class in which it will be used. If it will be used in Parent class then it will give name of the parent class only.
class p_class{
function test_p_class(){
echo __CLASS__;//will always print p_class
}
}
class test extends p_class
{
public function __construct() {
;
}
}
$t = new test;
$t->test_p_class();

__TRAIT__ Magic constant in PHP

Introduced from PHP 5.4.0 and give name of the trait it is used. If will be used outside the trait it will return blank string.

For example:
trait test_trait{
function magic_const(){
echo __TRAIT__;//will print test_trait
}
}
class test{
use test_trait;
}
$a = new test;
$a->magic_const();

__METHOD__ Magic constant in PHP

__METHOD__ will give name of the class method where it is used.
For example:
class test{
public function __construct() {
echo __METHOD__;//print test::__construct
}
public function test_m(){
echo __METHOD__;//print test::test_m
}
}
$a = new test;
$a->test_m();

In case of inheritance? Still will return name of the class where it is used, if it is used in parent then it will give parent class name for example
class p_class{
function test_p_m(){
echo __METHOD__; //will print p_class::test_p_m
}
}
class test extends p_class{
public function __construct() {
echo __METHOD__;//prints test::__construct
}
public function test_m(){
echo __METHOD__;//test::test_m
}
}
$a = new test;
$a->test_m();
$a->test_p_m();

__NAMESPACE__ Magic constant in PHP

__NAMESPACE__ will give name of the namespace where it is used. For example:
namespace testns;
class test{
public function __construct() {
echo 'constructor is called';
}
}
$cls_nm= __NAMESPACE__ .'\test';
$a = new $cls_nm;

Download the code used in Tutorial

To read further on Magic Constants You may refer to http://php.net/manual/en/language.constants.predefined.php

Support Me by Sharing This Article

Ankur Kumar Singh

I am a PHP programmer having some knowledge about Linux. I am always interested in web development and knowledge sharing. I am full time tech evangelist part time human being. :-)

Leave a comment/Ask Question

One thought on “Magic Constants in PHP Programming

shares