PHP Magic Constants: Usage and Debugging with Examples

Constants play an important role for writing different PHP scripts. I believe, it is very good and professional approach to design any PHP script in a way that can easily be configured on different environments like Linux or Windows – and it can easily be achieved by defining useful constants in “config.php” or any other file depending on the script nature.

Thanks to PHP Magic Constants who have made my life easy for writing an easy to use config files in my free WordPress themes i.e Chip Zero, Chip Life, Chip Photo and  PHP classes i.e Chip File Download, Chip File Upload, Chip Password Generator and Chip CSV Parser.

Let’s learn about PHP Magic Constants one by one,

1. __LINE__

The current line number of the file.

It is a constant of dynamic nature as it depends on the line that it’s used on in your script. It is very useful constant for debugging or any other purpose.

Example

PHP Magic Constants __LINE__

Preview

Line Number: 11

So you can easily get information about the line number of PHP script in your file

2. __FILE__

The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, __FILE__ always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances.

It is one of my favorite PHP Magic Constant, as i have used it several times in my different PHP scripts. It is very helpful to calculate path of your script file.

Example

<?php
echo __FILE__;
?>

Preview

E:\wamp\www\tutorialchip\magic_constants.php

3. __DIR__

The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__). This directory name does not have a trailing slash unless it is the root directory. (Added in PHP 5.3.0.)

Great, It is really a powerful PHP Magic Constant. It resolves the headache of calculating directory paths as the entire PHP application depends on the correct path. Let’s have an idea,

Example

<?php
echo __DIR__;
?>

Preview

E:\wamp\www\tutorialchip

As a PHP developer, is it not a great blessing ?

4. __FUNCTION__

The function name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the function name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.

This interesting PHP Magic Constant gives the function name.

Example

<?php
/**
Sample Function
*/

function sayHello () {
	echo "Hello by " . __FUNCTION__ . " function.";
}

/**
Function Call
*/

sayHello();
?>

Preview

Hello by sayHello function.

5. __CLASS__

The class name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the class name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.

This PHP Magic Constant will output the class name.

Example

<?php
/**
Sample Class
*/

class TutorialChip {

	/**
	Class Constructor
	*/

	function __construct() {
		echo "I am constructor of " . __CLASS__ . " class.";
	}

}

/**
Class Object
*/

$object = new TutorialChip();

?>

Preview

I am constructor of TutorialChip class.

6. __METHOD__

The class method name. (Added in PHP 5.0.0) The method name is returned as it was declared (case-sensitive).

This PHP Magic Constant will provide you information about class method. Let’s have a look,

Example

<?php
/**
Sample Class
*/

class TutorialChip {

	/**
	Class Constructor
	*/

	function __construct() {
		echo "I am constructor of " . __CLASS__ . " class.";
	}

	/**
	Say Hello by Class Method
	*/

	function sayHello() {
		echo "<br />Hello by " . __METHOD__;
	}

}

/**
Class Object
*/

$object = new TutorialChip();
$object->sayHello();

?>

Preview

I am constructor of TutorialChip class.
Hello by TutorialChip::sayHello

7. __NAMESPACE__

The name of the current namespace (case-sensitive). This constant is defined in compile-time (Added in PHP 5.3.0).

Example

<?php
namespace TutorialChip;

/**
Namespace
*/

echo "Namespace name is " . __NAMESPACE__;
?>

Preview

Namespace name is 'TutorialChip'.

I hope you have enjoyed the article and don’t forget to subscribe your email to stay tuned with incoming tutorials.