jump to navigation

PHP Magic Constants October 10, 2010

Posted by Tournas Dimitrios in PHP.
trackback

There are several PHP “magic constants” (or “magical contants”) which can be useful for a variety of reasons. These magic constants aren’t actually constants at all, but effectively behave like them, although the values change depending on the context.

  • __FILE__
  • __DIR__
  • __LINE__
  • __CLASS__
  • __METHOD__
  • __FUNCTION__
  • __NAMESPACE__

These are as follows.

__FILE__

__FILE__ is the full path and file name of the file that is being parsed. This can be useful for debugging purposes, and also for determining the absolute path of the current directory when used in conjunction with the dirname() function. For example, if the file that is echoing out __FILE__ is in /var/www/htdocs/example.php, the following would be output:

// this is relative to the loaded script's path
// it may cause problems when running scripts from different directories
require_once('config/database.php');

// this is always relative to this file's path
// no matter where it was included from
require_once(dirname(__FILE__) . '/config/database.php');

echo __FILE__;
//Example output:
/var/www/htdocs/example.php

echo dirname(__FILE__);
//Example output:
/var/www/htdocs

Note that if the file __FILE__ is used in is an include file, then the value of __FILE__ is the name of the include file, not the script that includes the file.

__DIR__

__DIR__ contains the directory of the file it is in. If the file is an include, it is the directory that include file is in. If it is the main script it is the directory that script is in.

__DIR__ is available from PHP 5.3; unless your application is designed to work only from PHP 5.3 or later, use the more traditional dirname(__FILE__) instead.

__LINE__

__LINE__ is the current line number of the file that is being parsed. This can be useful for debugging purposes.

Note that if the file __LINE__ is used in is an include file, then the value of __LINE__ is the line number of the include file, not the script that includes the file.

__CLASS__

Class is the class name. In PHP5 the value is case-sensitive and will be the exact case matched value of the class name; in PHP4 the value will be in lower case. As the following examples show, __CLASS__ is the class name of the class that it is called in; when calling a method from the parent’s class, the parent’s class is used.

class foo {
  function bar() {
    echo __CLASS__ . '
';
  }
}
class bar extends foo {
  function baz() {
    echo __CLASS__ . '
';
  }
}
foo::bar(); // echos 'foo'
bar::bar(); // echos 'foo'
bar::baz(); // echos 'bar'

__METHOD__

__METHOD__ is available from PHP 5.0, and is the name of the current method. It is returned as it was declared so is case sensitive.

class foo {
  function bar() {
    echo __METHOD__ . '
';
  }
}

// the example below echos foo::bar
foo::bar();

// the example below echos foo::bar
$foo = new foo();
$foo->bar();

__FUNCTION__

__FUNCTION__ is the fuction name of the current function, and works for both class methods and regular functions. In PHP5 the value is case-sensitive and will be the exact case matched value of the function name; in PHP4 the value will be in lower case.

The first example below shows using __FUNCTION__ in a class method; the second from a regular function.

class foo {
  function bar() {
    echo __FUNCTION__ . '
';
  }
}

// the example below echos bar
foo::bar();

function bar() {
  echo __FUNCTION__ . '
';
}

// the example below echos bar
bar();

__NAMESPACE__

Namespaces are one of the most significant changes in PHP 5.3. They will be familiar to C# and Java developers, and they are likely to change the structure of PHP applications for the better.This statement deserves an separeted post ,so a future tutorial will go in more depth .

This is example PHP script with comments, which demonstrate howto use all previously mentioned PHP Magic Constants.

< ? p h p   	
 // Set namespace (works only with PHP 5.3) 	
 namespace TestProject;   	 
// This prints file full path and name 	
 echo "This file full path and file name is '" . __FILE__ . "'.\n";   	 
// This prints file full path, without file name 	
 echo "This file full path is '" . __DIR__ . "'.\n";   	 
// This prints current line number on file 	
 echo "This is line number " . __LINE__ . ".\n";   	
 // Really simple basic test function 	
 function test_function_magic_constant() {  	
	echo "This is from '" . __FUNCTION__ . "' function.\n";  
	}   	 
// Prints function and used namespace  
	test_function_magic_constant();   	 
// Really simple class for testing magic constants 	
 class TestMagicConstants { 		 
// Prints class name 	 
	public function printClassName() { 		 
	echo "This is " . __CLASS__ . " class.\n"; 	 
	}   		 
// Prints class and method name 	
 	public function printMethodName() { 		 
	echo "This is " . __METHOD__ . " method.\n"; 	
 	}   		 
// Prints function name 	 
	public function printFunction() { 	 	
	echo "This is function '" . __FUNCTION__ . "' inside class.\n"; 		}   		 
// Prints namespace name (works only with PHP 5.3) 	
	 public function printNamespace() { 		 
	echo "Namespace name is '" . __NAMESPACE__ . "'.\n"; 	
 	}  
	}   	
 // Create new TestMagicConstants class 	
 $test_magic_constants = new TestMagicConstants;   	 
// This prints class name and used namespace 	$test_magic_constants->printClassName();

	// This prints method name and used namespace
	$test_magic_constants->printMethodName();

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

	// This prints namespace name (works only with PHP 5.3)
	$test_magic_constants->printNamespace();

?>

Output :

This file full path and file name is '/home/dimitrios/tmp/magic_constants/magic.php'.
This file full path is '/home/dimitrios/tmp/magic_constants'.
This is line number 13.
This is from 'TestProject\test_function_magic_constant' function.
This is TestProject\TestMagicConstants class.
This is TestProject\TestMagicConstants::printMethodName method.
This is function 'printFunction' inside class.
Namespace name is 'TestProject'.

The PHP manual reference page for magic constants is at http://www.php.net/manual/en/language.constants.predefined.php

Advertisements

Comments»

1. PHP Namespaces what are they and how to use theme « Tournas Dimitrios - October 10, 2010

[…] how to use theme October 10, 2010 Posted by tournasdimitrios1 in Uncategorized. trackback A earlier post mentioned briefly the “__NAMESPACE__” constant .So let’s get deeper in this […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s