Magic Constants in PHP : Tutorial

PHP provides a list of predifined constansts.

Most of these constants are created by various extensions, and will only be present when those extensions are available, either via dynamic loading or because they have been compiled in.

PHP provides eight magical constants that change depending on where they are used.

For example, the value of __LINE__ magic constant changes depending on the line which it is being used.

1. __LINE__

 - Displays the current line number of the file.

2. __FILE__

 Displays the full path and filename of the file.
   If used inside an include, the name of the included file is displayed. 
3. __DIR__

 Displays the directory of the file.
   If used inside an include, the directory of the included file is returned.

  Displays the function name.
5. __CLASS__

  Displays the class name.
6. __TRAIT__

  Displays the trait name.
7. __METHOD__

  Displays the name of the class and the method name.

  Displays the namespace.
Example :

echo "__LINE__ = ".__LINE__."<br>";
echo "__FILE__ = ".__FILE__."<br>";
echo "__DIR__ = ".__DIR__."<br>";

class Foo {
	function Test() {
		echo "__METHOD__ = ".__METHOD__."<br>";
		echo "__FUNCTION__ = ".__FUNCTION__."<br>";
		echo "__CLASS__ = ".__CLASS__."<br>";

$foo = new Foo;


__LINE__ = 3 __FILE__ = I:\xampp\htdocs\xampp\Tutorials\magicConstantsTest.php __DIR__ = I:\xampp\htdocs\xampp\Tutorials __METHOD__ = Foo::Test __FUNCTION__ = Test __CLASS__ = Foo