HomeCoding & Programming

PHP Magic Predefined Constants & usages

PHP Magic Predefined Constants & usages
Like Tweet Pin it Share Share Email

PHP Magic Predefined Constants & usages

In PHP, a constant is a name or an identifier for a simple value. A constant name starts with a letter or underscore can be followed by any number of letters, numbers, or underscores. If you have defined a constant, it can never be changed or undefined.

 

You have to use define() function to put value in the constant and for retrieval the value simply specifying the name.
The constant() function can be used if you create the constant name dynamically and want to fetch the value of the constant.

 

<?php
define("SITENAME", 'scriptarticle.com');
echo SITENAME;
echo constant("SITENAME"); // returns the same as previous one
?>

 

Along with the above PHP itself have a lot of predefined constants.

Some of the constants that are specials and can be called as magical are listed as below with example, these starts and ends with double underscore (__) and written in capital letters.

The predefined constants are very useful to access information about your code.

 

__LINE__

returns the line number in the code where the constant appears.

 

__DIR__

represents the path to the current file.

 

__CLASS__

returns current class name.

 

__FUNCTION__

returns current function name.

 

__METHOD__

represents the current method name.

 

__NAMESPACE__

returns the current namespace name.

 

__FILE__

represents the name of your file, includes its full path.

 

I have included all these in the below example, have a look.

<?php

	// Set namespace (works only with PHP 5.3 and later)
	namespace Scriptarticle;

	// this echo current file's full path and name
	echo "This file full path and file name is '" . __FILE__;

	// this echo file full path
	echo "This file full path is '" . __DIR__;

	// This echo current line number on file
	echo "This is line number " . __LINE__;

	function function_magic_constant() {
		echo "This is from '" . __FUNCTION__ . "' function.";
	}

	// echo function and used namespace
	function_magic_constant();

	class SAConstants {
		// echo class name
		public function printClassName() {
			echo "This is " . __CLASS__ . " class";
		}
		// echo class and method name
		public function printMethodName() {
			echo "This is " . __METHOD__ . " method";
		}
		// echo function name
		public function printFunction() {
			echo "This is function '" . __FUNCTION__ . "' inside class";
		}
		public function printNamespace() {
			echo "Namespace name is '" . __NAMESPACE__ . "'";
		}
	}

	// create a object of the class
	$obj = new SAConstants;

	$obj->printClassName();

	$obj->printMethodName();

	// this prints function name inside class and used namespace
	// same as method name, but without class
	$obj->printFunction();

	// this prints namespace name
	$obj->printNamespace();

?>

 

If you want to view a complete list of PHP predefined constant then follow the below links.

PHP Config Predefined Constants
PHP Image Predefined Constants
PHP Date Predefined Constants
PHP Core Predefined Constants
PHP Magic constants

 

Hope you have enjoyed the post!!