PHP Constants-Magic Constants

Source: Internet
Author: User

There are seven magic constants in PHP, their values are actually changing, and their values change as they change position in the code. So they are called magic constants. For example, the value of __line__ depends on the row it is in the script to determine. These special constants are case insensitive. A brief description of these variables in the manual is as follows:

name Description
__line__ The current line number in the file
__file__ The full path and file name of the file. If used in the included file, returns the file name that is included. From PHP 4.0.2,FILE always contains an absolute path (if it is a symbolic connection, the resolved absolute path), and the previous version sometimes contains a relative path.
__dir__ The directory where the file resides. If used in the included file, returns the directory where the included files are located. It is equivalent to DirName (FILE). Unless it is a root directory, the name in the directory does not include the trailing slash. (New in PHP 5.3.0)
__function__ The name of the function (PHP 4.3.0 new addition). From PHP 5 This constant returns the name (case-sensitive) when the function is defined. In PHP 4, this value is always lowercase > alphabetic
__class__ The name of the class (PHP 4.3.0 new addition). From PHP 5 This constant returns the name of the class when it is defined (case-sensitive). In PHP 4, this value is always in lowercase letters.
__method__ The method name of the class (PHP 5.0.0 new addition). Returns the name of the method when it is defined (case-sensitive).
__namespace__ The name of the current namespace (case sensitive). This constant is defined at compile time (PHP 5.3.0 new)

Some of the more magical variables or markers in PHP are used to differentiate by using underscores, so try not to define the constants at the beginning of the double downline when writing PHP code.

Instead of parsing at runtime, the PHP kernel replaces the content assignments of these constants in lexical parsing, in the case of __function__, in the Zend/zend_language_scanner.l file, __function__ is a meta tag (token) that needs to be analyzed:

1<ST_IN_SCRIPTING>"__function__"{Char*func_name =NULL;2     if(CG (Active_op_array)) {3Func_name = CG (Active_op_array)function_name; } 4     if(!func_name) {Func_name =""; }5Zendlval->value.str.len =strlen (func_name);6Zendlval->value.str.val = Estrndup (Func_name, zendlval->Value.str.len);7Zendlval->type = is_string;returnT_func_c;}

Right here, when the current middle code is in a function, the current function name is assigned to Zendlval (that is, the token T_FUNC_C 's value content), and if not, an empty string is assigned to Zendlval (so print directly in the top-level role domain name __function__ Will output a space). This value is directly assigned to the return value when parsing. So we can see in the generated intermediate code that the positions of these constants have been assigned values.

These constants are actually equivalent to a constant template, or a placeholder, in which the templates or placeholders are replaced with actual values when parsing the lexical

For more information, you can see tipi

PHP Constants-Magic Constants

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.