在php中,有很多的系统常量,比如取当前的行号 (__LINE__),文件 (__FILE__),目录 (__DIR__),函数名 (__FUNCTION__),类名(__CLASS__),方法名(__METHOD__) 和名
字空间 (__NAMESPACE__)等。
以上常量,可以用于调试,也可以用于一些特殊用途。
比如,可以在include其它文件的时候使用?__FILE__ (当然,也可以在 PHP 5.3以后使用 __DIR__ )。
例子:
/**
* php 获取系统常量
* by www.jbxue.com
*/
// 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’);
?>
使用 __LINE__ 来输出一些debug信息:
// some code
// …
my_debug(“some debug message”, __LINE__);
/* 输出
Line 4: some debug message
*/
// some more code
// …
my_debug(“another debug message”, __LINE__);
/* 输出
Line 11: another debug message
*/
function my_debug($msg, $line) {
echo “Line $line: $msg\n”;
}
?>