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:
1 2 3 | 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.
1 2 3 4 |
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.
1 2 3 4 | 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.
1 2 3 4 | 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.
1 2 3 4 5 6 7 8 | 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.
1 2 3 4 5 6 7 8 9 10 11 | 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?
1 2 3 4 5 6 7 8 9 10 11 12 | 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
1 2 3 4 5 6 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | class A { public function funcA() { } } trait Test_trait { public function 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.