PHP Magic Constants
Posted December 8th, 2007 in PHP
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.
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:
Example code: echo __FILE__; Example output: /var/www/htdocs/example.php Example code: 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.
__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__ . '<br />';
}
}
class bar extends foo {
function baz() {
echo __CLASS__ . '<br />';
}
}
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__ . '<br />';
}
}
// 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__ . '<br />';
}
}
// the example below echos bar
foo::bar();
function bar() {
echo __FUNCTION__ . '<br />';
}
// the example below echos bar
bar();
The PHP manual reference page for magic constants is at http://www.php.net/manual/en/language.constants.predefined.php
Subscribe!
If you found this post interesting and would like to be notified the next time something is posted, please subscribe to my RSS Feed. Thanks for visiting!

