When explaining PHP constants, we know that most of the constants in PHP are unchanged, but there are eight predefined constants that will change with the location of their code, these eight constants are called magic constants. these special constants are case-insensitive,
What is a magic constant?
When explaining PHP constants, we know that most of the constants in PHP are unchanged, but there are eight predefined constants that will change with the location of their code, these eight constants are called magic constants. these special constants are case-insensitive,
The following table lists the "magic constants" of PHP.
Function |
Detection type |
_ LINE __ |
The current row number in the file. |
_ FILE __ |
The complete file path and file name. If it is used in a file to be included, the file name to be included is returned. Starting from PHP 4.0.2, __file _ always contains an absolute path (if it is a symbolic connection, it is an absolute path after resolution ), earlier versions sometimes contain a relative path. |
_ DIR __ |
The directory where the file is located. If it is used in included files, the Directory of the included files is returned. It is equivalent to dirname (_ FILE __). Unless it is the root directory, the name of the directory does not include the slash at the end. (Added in PHP 5.3.0) |
_ FUNCTION __ |
Function name (new PHP 4.3.0 ). Starting from PHP 5, this constant returns the name (case sensitive) when the function is defined ). In PHP 4, the value is always lowercase letters. |
_ CLASS __ |
Class name (new PHP 4.3.0 ). Starting from PHP 5, this constant returns the name (case sensitive) when the class is defined ). In PHP 4, the value is always lowercase letters. The class name includes the declared zone (for example, Foo \ Bar ). Note that _ CLASS _ from PHP 5.4 also applies to trait. When used in the trait method, __class _ is the name of the CLASS that calls the trait method |
_ TRAIT __ |
Trait name (new in PHP 5.4.0 ). Starting from PHP 5.4, this constant returns the name (case sensitive) of the trait definition ). The Trait name includes the declared region (for example, Foo \ Bar) |
_ METHOD __ |
Class method name (new PHP 5.0.0 ). Returns the name (case sensitive) when the method is defined ). |
_ NAMESPACE __ |
Name of the current namespace (case sensitive ). This constant is defined during compilation (new in PHP 5.3.0 ). |
In the table above, "_" is two underscores rather than one "_".
Magic constants are often used to obtain information about the current environment or record logs.
Let me take a look at the usage of these magic constants:
_ LINE _ get the current row number in the file.
Instance
";?>
Code running result:
_ DIR _ get the directory where the file is located. If it is used in included files, the Directory of the included files is returned.
Instance
";?>
Code running result:
_ FUNCTION _ obtain the name (case sensitive) when the FUNCTION is defined. only the FUNCTION name is returned.
Instance
";}Test () ;?>
Code running result:
"; Echo" this FUNCTION name is: ". _ FUNCTION _; echo"
";}}$ T = new test (); $ t-> show ();?>
Code running result:
_ METHOD _ get the name (case sensitive) when the METHOD is defined. if the function is in the class, the class METHOD name is returned,
That is, the class name: method name.
Instance
";}Test1 () ;?>
Test (); // output: chhua: test?>
Code running result:
_ NAMESPACE _ name of the current NAMESPACE (case sensitive)
Code running result:
The above is a detailed description of the magic constant instance in php. For more information, see other related articles in the first PHP community!