PHP Source Definition Constants

Source: Internet
Author: User

Constants, as the name implies, are a normal measure. It is bound to the value only once, it is to have a rib to increase the readability and reliability of the program. In PHP, the name of a constant is the identifier of a simple value that cannot be changed during script execution. As with variables, constants are case-sensitive by default , but are always capitalized according to our customary constant identifiers. The constant name and any other PHP tags follow the same naming conventions.

PHP 7 and the previous version of the implementation is not the same, the following first 5.3.5

The storage structure of constants in PHP is defined as 33 lines in the Zend/zend_constants.h file

typedef struct _ZEND_CONSTANT {
Zval value; /* Zval structure, the storage structure of PHP internal variables */INT flags; /* tags for constants such as Const_persistent | Const_cs * *
Char *name; /* Constant Name */
UINT Name_len;
int module_number; /* Module Number */

}zend_constant;

The function used to define constants is define, and this ambiguous implementation is in the ZEND/ZEND_BUILTIN_FUNCTIONS.C

Char *name;
int Name_len;
Zval *val;
Zval *val_free = NULL;
Zend_bool non_cs = 0;
int case_sensitive = Const_cs;
Zend_constant C;

if (Zend_parse_parameters (Zend_num_args () tsrmls_cc, "Sz|b", &name, &name_len, &val, &non_cs) = = FAILURE) {
Return
}

if (Non_cs) {
case_sensitive = 0;
}

One of the main functions here is zend_parse_parameters, the function of which is to solve the parameters.

In detail, this function, first look at the function of the more bizarre parameter Zend_num_args () TSRMLS_CC, which is a space between the parameters and not a comma,

In fact, this look at the TSRMLS_CC macro definition will be able to understand

#define TSRMLS_CC , Tsrmls_c //thread Safe mode

#define TSRMLS_CC //non-thread safe mode

That is, in the thread safe mode, TSRMLS_CC will be replaced , Tsrmls_c and will eventually be replaced, Tsrm_ls, sub-threaded Safe mode, is actually a space

The following is a description of the Tsrm_ls variable.

In the PHP source code often see some nouns, TSRM is the Thread Safety resource Manager (threads Safe Resource Manager), it is under the/TSRM directory of the source code. In general, this will only be used when the need is known to be enabled (for example, Apache2+worker MPM, a thread-based MPM), for Win32 under the Apache, is based on multithreading, so this layer is always enabled under Win32. ZTS is Zend thread safety (Zend threads Safety), and when TSRM is enabled, the macro named Zts is defined. Tsrm_ls is the TSRM memory (TSRM Local Storage), which is the variable name that is actually used in extensions and Zend for the reference tsrm stored. A macro that starts with Tsrmls_ typically has four to implement TSRM based on whether the ZTS macro is defined or not. 4 macros are as follows:

#define Tsrmls_c Tsrm_ls

#define TSRMLS_D void * * * Tsrm_ls

#define TSRMLS_CC, Tsrm_ls

#define Tsrmls_ds, void ***tsrm_ls //Note there is a comma

PHP source code with such a complex macro to ensure thread safety, which is mainly due to the global variables. To put it simply, if we are dealing with a variable in two functions, one way is to use the global, for example:

#include <stdio.h>

Char *message;

void output_func(void){

printf("%s\n", message);

 }

int main(int argv, char *argv[]){

Message = argv[0];

Output_func();

return 0;

}

Generic single-threaded models such as PHP CLI, Apache1, or Apache2+prefork MPM (also a multi-process model) can be used with confidence and without error. Global variables are created at Minit/rinit and then accessed during the entire process runtime/request processing period and then released at Mshutdow/rshutdown. But in a multithreaded model, this approach is not secure, such as Apache2+worker MPM and IIS. In this case, all threads share the address space of the same process, which means that multiple threads are sharing a global variable, and this time there is a competition. In the way of C programmers: The global variables at this time are non-thread-safe.

To solve this problem and be compatible with single-threaded mode, Zend uses a mechanism called "Non_global Globals". The main idea of this mechanism is that, for multithreaded models, whenever a new thread is created, it allocates a single piece of memory, which stores a copy of the global variable. And this memory will be strung together by a vector, managed by Zend.

This link is an example of http://www.luojisiwei-inc.com/archives/521

PHP Source Definition 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.