教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php魔术常量

php魔术常量

发布时间:2014-11-14   编辑:jiaochengji.com
php魔术常量

PHP 提供了获取当前行号 (__LINE__)、文件路径 (__FILE__)、目录路径 (__DIR__)、函数名 (__FUNCTION__)、类名 (__CLASS__)、方法名 (__METHOD__) 和命名空间 (__NAMESPACE__) 等有用的魔术常量。在这篇文章中不作一一介绍,但是我将告诉你一些用例。当包含其他脚本文件时,使用 __FILE__ 常量(或者使用 PHP5.3 新具有的 __DIR__ 常量):
 

复制代码 代码如下:

// 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__ 使得调试更为轻松。你可以跟踪到具体行号。
 

复制代码 代码如下:

// some code
// ...
my_debug("some debug message", __LINE__);
/* prints
Line 4: some debug message
*/

// some more code
// ...
my_debug("another debug message", __LINE__);
/* prints
Line 11: another debug message
*/

function my_debug($msg, $line) {
 echo "Line $line: $msg\n";
}

您可能感兴趣的文章:
PHP的__METHOD__魔术常量用法
PHP的魔术方法与魔术常量
PHP5.3中新增的魔术常量__DIR__
php魔术常量
了解PHP中的8个魔术常量
PHP之十六个魔术方法详解(总结)
光影魔术手给照片添加边框 光影魔术手照片边框添加教程
php中如何使用魔术变量__line__
php中的魔术方法一些学习笔记
PHP的数据类型和魔术常量

[关闭]
~ ~