Updated March 17, 2023
Introduction to PHP Magic Constants
In PHP Magic Constants, there are in total of eight constants that change their dependency based on where they are used. All these magical constants are resolved at compilation time and not like the constants that we use on a regular basis which we generally resolve at run time. These magical constants are case-insensitive. These constants are predefined constants and start with a double underscore (__) and also ends with a double underscore. These constants are the most practical and most useful constants in PHP. They are simple variables but have a predefined meaning to them. These constants are used to print the user-defined inputs and process the output to display on the screen.
Types of Magic Constants in PHP
There is a total of eight magic constants in PHP mentioned below:
- __LINE__: This constant is used to print the line number of the code where it is used on the output screen. It represents the current line number.
- __FILE__: This constant is used to print the file’s full file path and file name on the output screen.
- __DIR__: This constant is used to print the particular file’s full directory path on the output screen. It also has one more equivalent method to print the file’s directory path is dirname (__FILE__).
- __FUNCTION__: This constant will print the function name where it is currently being used on the output screen. If it is used inside the function, it will print the function name, and if it is used outside the function, it will return a blank.
- __CLASS__: This constant is used to print the class name where it is used on the output screen. If it is used inside the class, it will print the class name, and if it is used outside the function, it will return a blank.
- __TRAIT__: This constant is used where the trait name is used. If it is used inside the function, it will print the name, and if it is used outside the function, it will return a blank. The trait is used to print the namespace on the output screen.
- __METHOD__: This constant is used to print the name of the method defined inside the class where it is used on the output screen. It returns the name of the method that is declared in the code. If used inside the method or class, it will return the name of the method, and if it is used outside the function, it will return a blank.
- __NAMESPACE__: This constant is used to print the current namespace’s name on the output screen.
How Magic Constants Works in PHP?
Below are the examples of How Magic Constants work in PHP:
In PHP, we can use magic constants in very easy code, too difficult ones that we use on our daily basis. Let’s take an example to see how it works:
Example #1
Code:
<!DOCTYPE html>
<html>
<body>
<?php
echo "<h1>Example for __LINE__ constant</h1>";
echo "The line number is " . __LINE__ . "<br><br>";// prints the current line number i.e;7
?>
</body>
</html>
Output:
Example #2
Code:
<!DOCTYPE html>
<html>
<body>
<?php
echo "<h2>Example for __FILE__ constant</h2>";
echo __FILE__ . "<br><br>";//prints the full path of the file with extension
?>
</body>
</html>
Output:
Example #3
Code:
<!DOCTYPE html>
<html>
<body>
<?php
echo "<h3>Example for __DIR__ constant</h3>";
echo __DIR__ . "<br><br>";//prints the full path of the directory where the script is placed.
?>
</body>
</html>
Output:
Example #4
Code:
<!DOCTYPE html>
<html>
<body>
<?php
function amount()
{
echo 'the function name is '. __FUNCTION__ . "<br><br>";//the function name is amount.
}
amount();
?>
</body>
</html>
Output:
Example #5
Code:
<!DOCTYPE html>
<html>
<body>
<?php
//Using magic constant inside function.
function amount()
{
echo 'the function name is '. __FUNCTION__ . "<br><br>";//the function name is amount.
}
amount();
echo 'the function name is '. __FUNCTION__ ."<br><br>";
?>
</body>
</html>
Output:
Example #6
Code:
<!DOCTYPE html>
<html>
<body>
<?php
echo "<h2>Example for __CLASS__</h2>";
class xyz
{
public function __construct() {
;
}
function xyz_method()
{
echo __CLASS__ . "<br>";//prints the name of the class xyz mentioned above.
}
}
$a = new xyz;
$a->xyz_method();
?>
</body>
</html>
Output:
Example #7
Code:
<!DOCTYPE html>
<html>
<body>
<?php
class abc
{
function test_abc()
{
echo __CLASS__;//will always print parent class which is abc mentioned above.
}
}
class xyz extends abc
{
public function __vowels()
{
;
}
}
$b = new xyz;
$b->test_abc();
?>
</body>
</html>
Output:
Example #8
Code:
<!DOCTYPE html>
<html>
<body>
<?php
echo "<h4>Example for __TRAIT__</h4>";
trait create_trait
{
function trait()
{
echo __TRAIT__;//will print name of the trait create_trait mentioned above.
}
}
class new_class
{
use create_trait;
}
$c = new new_class;
$c-> trait ();
?>
</body>
</html>
Output:
Example #9
Code:
<!DOCTYPE html>
<html>
<body>
<?php
echo "<h2>Example for __METHOD__</h2>";
class method
{
public function __parameter() {
echo __METHOD__ . "<br><br>";//print method::__parameter
}
public function method_fun(){
echo __METHOD__;//print meth::method_fun
}
}
$z = new method;
$z->method_fun();
?>
</body>
</html>
Output:
The output of the respective functions is mentioned above. The line constant will print the current line of the file leela.php stored in the localhost. The file constant will print the file name along with the path, as shown in the output. The dir constant or dirname will print the current one’s directory path or the mentioned one: the method and class constant prints the method name and class name mentioned in the code. If the constants are mentioned outside of method and class, then it will not print anything on the screen as it is out of the scope, and similarly, the other constant’s output is mentioned above.
Conclusion
In this article, we learned all the magic constants of PHP and its usage. It can be used in small and tiny programs to big or large programs. Developers can use these constants for backtracking any issue as to where the error might have occurred. These constants will help the developers or users to check on the code as to where they are currently present.
Recommended Articles
This is a guide to PHP Magic Constants. Here we discuss types of magic constants in php and their works in magic constant in php with proper codes and outputs. You can also go through our other related articles to learn more-