PHP provides a large number of predefined constants to any script which it runs. Many of these constants, however, are created by various extensions, and will only be present when those extensions are available, either via dynamic loading or because they have been compiled in.
There are eight magical constants that change depending on where they are used. For example, the value of __LINE__
depends on the line that it’s used on in your script. These special constants are case-insensitive and are as follows:
Name | Description |
---|---|
__LINE__ |
The current line number of the file. |
__FILE__ |
The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, __FILE__ always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances. |
__DIR__ |
The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__). This directory name does not have a trailing slash unless it is the root directory. (Added in PHP 5.3.0.) |
__FUNCTION__ |
The function name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the function name as it was declared (case-sensitive). In PHP 4 its value is always lowercased. |
__CLASS__ |
The class name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the class name as it was declared (case-sensitive). In PHP 4 its value is always lowercased. The class name includes the namespace it was declared in (e.g. Foo\Bar). Note that as of PHP 5.4 __CLASS__ works also in traits. When used in a trait method, __CLASS__ is the name of the class the trait is used in. |
__TRAIT__ |
The trait name. (Added in PHP 5.4.0) As of PHP 5.4 this constant returns the trait as it was declared (case-sensitive). The trait name includes the namespace it was declared in (e.g. Foo\Bar). |
__METHOD__ |
The class method name. (Added in PHP 5.0.0) The method name is returned as it was declared (case-sensitive). |
__NAMESPACE__ |
The name of the current namespace (case-sensitive). This constant is defined in compile-time (Added in PHP 5.3.0). |
说明:一般经常使用的魔术常量是__FILE__ 和__DIR__,一般用在文件包含时构造包含文件路径。
注意:
1、You cannot check if a magic constant is defined. This means there is no point in checking if __DIR__ is defined then defining it. `defined('__DIR__')` always returns false. Defining __DIR__ will silently fail in PHP 5.3+. This could cause compatibility issues if your script includes other scripts.
Here is proof:
<?php echo (defined('__DIR__') ? '__DIR__ is defined' : '__DIR__ is NOT defined' . PHP_EOL); echo (defined('__FILE__') ? '__FILE__ is defined' : '__FILE__ is NOT defined' . PHP_EOL); echo (defined('PHP_VERSION') ? 'PHP_VERSION is defined' : 'PHP_VERSION is NOT defined') . PHP_EOL; echo 'PHP Version: ' . PHP_VERSION . PHP_EOL; ?>
Output:
__DIR__ is NOT defined
__FILE__ is NOT defined
PHP_VERSION is defined
PHP Version: 5.3.6
2、__FUNCTION__和__METHOD__的区别:
_FUNCTION__ returns only the name of the function
while as __METHOD__ returns the name of the class alongwith the name of the function
class trick { function doit() { echo __FUNCTION__; } function doitagain() { echo __METHOD__; } } $obj=new trick(); $obj->doit();
output will be —- doit
$obj->doitagain();
output will be —– trick::doitagain
ps:上面介绍的8个称为魔术常量,是因为这些变量的值会因为使用的位置不同而变化,这些变量使用defined()函数会返回FASE。除了魔术常量,PHP还预定了一些常量,如PHP_VERSION和PHP_OS等,这些变量使用defined()函数会返回TRUE。具体参考上面说明第一点。
PHP_VERSION:当前PHP解释器的版本号
PHP_OS:执行当前PHP解释器的操作系统名称
ps:
1、 defined()函数的参数是常量的名称,是字符串,如defined(‘PHP_VERSION’)而不是defined(PHP_VERSION)。
defined() 函数检查某常量是否存在。若常量存在,则返回 true,否则返回 false。
2、define()函数用于定义一个常量。
define(name,value,case_insensitive)。
case_insensitive 可选。规定常量的名称是否对大小写敏感。若设置为 true,则对大小写不敏感。默认是 false(大小写敏感)。
定义常量类似变量,不同之处在于:
- 在设定以后,常量的值无法更改
- 常量名不需要开头的美元符号 ($)
- 作用域不影响对常量的访问
- 常量值只能是字符串或数字