PHP Magical Constants

Dec 25, 2011 by

PHP Magical Constants

Constant ,name itself says its a constant means can’t change its value and magic constant name says its create magic in php application.PHP offers some predefined constants which can we use in many php scripts.A good php developer always develop clean and easy 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:


PHP
1
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 path of your file.


PHP
1
2
echo __FILE__;
// Prints D:xampphpdocsindex.php

2. __LINE__

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


PHP
1
2
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 path of directories.


PHP
1
2
echo __DIR__;
// Prints D:xampphtdocs

4. __FUNCTION__

Display function name and added in php 4.3.0 so let’s take an idea about this Magic constant


PHP
1
2
3
4
5
6
function fun_name()
{
echo __FUNCTION__;
}
fun_name();
// Prints fun_name

5. __CLASS__

Display the class name.Its one of the interesting Magic constant because it returns class name as a result.


PHP
1
2
3
4
5
6
7
8
9
class testClass
{
function __construct() {
echo __CLASS__;
}
}
$obj = new testClass();
// Prints testClass

6. __METHOD__

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


PHP
1
2
3
4
5
6
7
8
9
10
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 namespaceand its newly added feature in php 5.3


PHP
1
2
3
4
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 its added in new PHP in PHP 5.4


PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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


Related Posts

Share This

0 Comments

Trackbacks/Pingbacks

  1. PHP Magical Constants | PHP | Syngu - [...] Magic constant name says its create magic in php application.HP offers some predefined constants which can we use in ...

Leave a Comment

Top of Page