Magic Constants PHP

Magic Constants  

Magic Constants

Magic Constants

Magic constants are the predefined constants in PHP which get changed on the basis of their use. They start with double underscore (__) and ends with double underscore.

They are similar to other predefined constants but as they change their values with the context, they are called magic constants.

There are eight magical constants defined in the below table. They are case-insensitive.

Name Description
__LINE__ Represents current line number where it is used.
__FILE__ Represents full path and file name of the file. If it is used inside an include, name of included file is returned.
__DIR__ Represents full directory path of the file. Equivalent to dirname(__file__). It does not have a trailing slash unless it is a root directory. It also resolves symbolic link.
__FUNCTION__ Represents the function name where it is used. If it is used outside of any function, then it will return blank.
__CLASS__ Represents the class name where it is used. If it is used outside of any function, then it will return blank.
__TRAIT__ Represents the trait name where it is used. If it is used outside of any function, then it will return blank. It includes namespace it was declared in.
__METHOD__ Represents the name of the class method where it is used. The method name is returned as it was declared.
__NAMESPACE__ Represents the name of the current namespace.

Example

Let's see an example for each of the above magical constants.

File Name: magic.php

  1. <?php  
  2. echo "<h3>Example for __LINE__</h3>";  
  3. echo "You are at line number " . __LINE__ . "<br><br>";// print Your current line number i.e;3  
  4. echo "<h3>Example for __FILE__</h3>";  
  5. echo __FILE__ . "<br><br>";//print full path of file with .php extension  
  6. echo "<h3>Example for __DIR__</h3>";  
  7. echo __DIR__ . "<br><br>";//print full path of directory where script will be placed  
  8. echo dirname(__FILE__) . "<br><br>"; //its output is equivalent to above one.  
  9. echo "<h3>Example for __FUNCTION__</h3>";  
  10. //Using magic constant inside function.  
  11. function cash(){  
  12. echo 'the function name is '. __FUNCTION__ . "<br><br>";//the function name is cash.  
  13. }  
  14. cash();  
  15. //Using magic constant outside function gives the blank output.  
  16. function test_function(){  
  17. echo 'HYIIII';  
  18. }  
  19. test_function();  
  20. echo  __FUNCTION__ . "<br><br>";//gives the blank output.  
  21.   
  22. echo "<h3>Example for __CLASS__</h3>";  
  23. class abc  
  24. {  
  25. public function __construct() {  
  26. ;  
  27. }  
  28. function abc_method(){  
  29. echo __CLASS__ . "<br><br>";//print name of the class abc.  
  30. }  
  31. }  
  32. $t = new abc;  
  33. $t->abc_method();  
  34. class first{  
  35. function test_first(){  
  36. echo __CLASS__;//will always print parent class which is first here.  
  37. }  
  38. }  
  39. class second extends first  
  40. {  
  41. public function __construct() {  
  42. ;  
  43. }  
  44. }  
  45. $t = new second;  
  46. $t->test_first();  
  47. echo "<h3>Example for __TRAIT__</h3>";  
  48. trait created_trait{  
  49. function abc(){  
  50. echo __TRAIT__;//will print name of the trait created_trait  
  51. }  
  52. }  
  53. class anew{  
  54. use created_trait;  
  55. }  
  56. $a = new anew;  
  57. $a->abc();  
  58. echo "<h3>Example for __METHOD__</h3>";  
  59. class meth{  
  60. public function __construct() {  
  61. echo __METHOD__ . "<br><br>";//print meth::__construct  
  62. }  
  63. public function meth_fun(){  
  64. echo __METHOD__;//print meth::meth_fun  
  65. }  
  66. }  
  67. $a = new meth;  
  68. $a->meth_fun();  
  69.   
  70. echo "<h3>Example for __NAMESPACE__</h3>";  
  71. class name{  
  72. public function __construct() {  
  73. echo 'This line will be printed on calling namespace';  
  74. }  
  75. }  
  76. $clas_name= __NAMESPACE__ .'\name';  
  77. $a = new $clas_name;  
  78. ?>  

Output:

PHP Magic constants 1

Download free E-book of PHP


#askProgrammers
Learn Programming for Free


Join Programmers Community on Telegram


Talk with Experienced Programmers


Just drop a message, we will solve your queries