PHP core technology and best practices create variables in PHP extensions-php Tutorial

Source: Internet
Author: User
Create a variable in PHP extension in PHP core technology and best practices 1) create a local variable: at the PHP language layer, as long as the variable is not declared using global, it is a local variable. When writing extensions, to create a local variable that can be accessed by PHP scripts, you must first create a zval container and then fill in the zval window as necessary, finally, introduce it to the internal symbol table of the Zend Engine. the code is as follows:

Zval * new_var;

// Apply for and initialize a new zval container

MAKE_STD_ZVAL (new_var );

// Introduce the "new_var" variable to the current active symbol table

ZEND_SET_SYMBOL (EG (active_symbol_table), "new_var", new_var );

// Now you can use $ new_var in the script to access this variable.

MAKE_STD_ZVAL () macro will apply a new zval container's memory space through ALLOC_ZVAL () macro and call the INIT_PVAL () macro to initialize the zval container. the MAKE_STD_ZVAL macro code is as follows:

# Define MAKE_STD_ZVAL (zv)

ALLOC_ZVAL (zv );

INIT_PZVAL () zv;

The INIT_PVAL () macro sets the zval container's reference counter (refcount) to 1, and sets the reference ID to 0. the INIT_PVAL () macro code is as follows:

# Define INIT_PZVAL (z)

(Z)-> refcount = 1;

(Z)-> is_ref = 0;

After creating a zval container, you must introduce it to the symbol table for use in the PHP script. the ZEND_SET_SYMBOL () macro is responsible for introducing the new variable to the symbol table in the Zend Engine. This macro will first check whether the variable already exists in the symbol table. if it already exists, it will replace it with another referenced variable and the original zval container will be automatically destroyed.

The ZEND_SET_SYMBOL () macro accesses the global structure of the Zend executor through the EG macro (the part where the Zend executor executes Opcodes). If the EG (active_symbol_table) is used ), you can access the current active symbol table (usually the local variable table) to process some local variables.

If you are very concerned about program running efficiency and do not care about memory, you can use the zend_hash_update () function to check whether the variable name exists in the symbol table and forcibly insert the variable name into the symbol table. the code is as follows:

Zval * new_var;

MAKE_STD_ZVAL (new_var );

Zend_hash_update (

EG (active_symbol_table ),

"New_var ",

Sizeof ("new_var "),

& New_var,

Sizeof (zval *),

NULL

);

2) create a global variable:

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.