Home > Article > Backend Development > What are the magic constants in php?
In php we can customize constants. At the same time, php has also customized some useful constants. These constants will automatically change according to their location. We call it Magic Constant.
There are eight magic constants in php:
LINE The current line number in the file.
#FILE The full path and file name of the file. If used in an included file, returns the included file name. Since PHP 4.0.2, FILE always contains an absolute path (or the resolved absolute path in the case of a symbolic link), while versions before that sometimes contained a relative path.
#DIR The directory where the file is located. If used within an included file, returns the directory where the included file is located. It is equivalent to dirname(FILE). Directory names do not include the trailing slash unless they are the root directory. (New in PHP 5.3.0) =
FUNCTION Function name (New in PHP 4.3.0). Since PHP 5 this constant returns the name of the function as it was defined (case sensitive). In PHP 4 this value is always lowercase.
CLASS The name of the class (newly added in PHP 4.3.0). Since PHP 5 this constant returns the name of the class when it was defined (case sensitive). In PHP 4 this value is always lowercase. The class name includes the scope in which it is declared (e.g. Foo\Bar). Note that as of PHP 5.4 CLASS also works for traits. When used within a trait method, CLASS is the name of the class that calls the trait method.
TRAIT The name of Trait (newly added in PHP 5.4.0). Since PHP 5.4 this constant returns the name of the trait as it was defined (case-sensitive). The trait name includes the scope in which it is declared (e.g. Foo\Bar).
METHOD The method name of the class (newly added in PHP 5.0.0). Returns the name of the method as it was defined (case-sensitive).
NAMESPACE The name of the current namespace (case-sensitive). This constant is defined at compile time (new in PHP 5.3.0).
The above is the detailed content of What are the magic constants in php?. For more information, please follow other related articles on the PHP Chinese website!