PHP extension summary

Source: Internet
Author: User
: This article mainly introduces the PHP extension summary. if you are interested in the PHP Tutorial, you can refer to it. Macro definition of thread security

TSRM/TSRM. the hfile has the following definitions # define TSRMLS_FETCH () void *** tsrm_ls = (void ***) ts_resource_ex (0, NULL) # define interval *** tsrm_ls # define TSRMLS_DC, TSRMLS_D # define TSRMLS_Ctsrm_ls # define TSRMLS_CC, TSRMLS_C
1. add TSRMLS_D or TSRMLS_DC (with parameters) to the method definition)
2. when calling a method (real parameter), use TSRMLS_C or TSRMLS_CC (with parameters)
Variable-related macros (partial)
SG-> SAPI Global SAPI information (main/SAPI. h) EG-> Executor Global execution information (zend/zend_globals_macros.h: 43) PG-> PHP Core Global mainly stores php. information in ini: PHP global variables. Php. ini maps one or more global structures of PHP. (Main/php_globals.h) FG-> File Global variable. Data streams of most file I/O or related global variables are inserted into the standard extended exit structure. (Ext/standard/file. h) CG-> Complier Global compile-time information, including function table (zend_globals_macros.h: 32) used to access core Global variables. (Zend/zend_globals_macros.h)

File macro:
Getcwd () VCWD_GETCWD () fopen () VCWD_FOPENopen () VCWD_OPEN () // open () VCWD_OPEN_MODE () for two parameter versions // open () VCWD_OPEN_MODE () for three parameters () version creat () VCWD_CREAT () chdir () VCWD_CHDIR () getwd () VCWD_GETWD () realpath () VCWD_REALPATH () rename () VCWD_RENAME () stat () VCWD_STAT () lstat () round () unlink () VCWD_UNLINK () mkdir () VCWD_MKDIR () rmdir () VCWD_RMDIR () opendir () VCWD_OPENDIR () popen () VCWD_POPEN () access () VCWD_ACCESS () utime () VCWD_UTIME () chmod () VCWD_CHMOD () chown () VCWD_CHOWN ()

Global variables:
# Php-fpm generates POST | GET | COOKIE | SERVER | ENV | REQUEST | file global variable process php_cgi_startup ()-> php_module_startup ()-> php_startup_auto_globals () -> save the variable to the symbol_table symbol table php_cgi_startup (). In fpm/fpm_main.c, define php_module_startup () in main/main. define php_startup_auto_globals () in main/php_variables.h define zend_hash_update (& EG (symbol_table), "_ GET", sizeof ("_ GET") + 1, & vars, sizeof (zval *), NULL);/* read $ _ SERVER variable */static PHP_FUNCTION (print_server_vars) {zval ** val; if (zend_hash_find (& EG (symbol_table), "_ SERVER", sizeof ("_ SERVER"), (void **) & val) = SUCCESS) {RETURN_ZVAL (* val, 1, 0);} else {RETURN_FALSE ;}} /* read $ _ SERVER [$ name] */outputs (print_server_var_arginfo, 0) ZEND_ARG_INFO (0, "name") ZEND_END_ARG_INFO () static PHP_FUNCTION (print_server_var) {char * name; int name_len; zval ** val; HashTable * ht _ Vars = NULL; HashPosition pos; zval ** ret_val; if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "| s! ", & Name, & name_len) = FAILURE) {RETURN_NULL ();} if (zend_hash_find (& EG (symbol_table)," _ SERVER ", sizeof ("_ SERVER"), (void **) & val) = SUCCESS) {ht_vars = Z_ARRVAL_PP (val ); // enter a value greater than name length + 1, because the string value must be '\ 0' if (zend_hash_find (ht_vars, name, name_len + 1, (void **) & ret_val) = SUCCESS) {RETURN_STRING (Z_STRVAL_PP (ret_val), 0) ;}else {RETURN_NULL () ;}} else {RETURN_NULL ();}}

Third-party library packaging:
SEARCH_PATH = "/usr/local/usr" # SEARCH_FOR = "/include/curl. h "# lib header file path if test-r $ PHP_LIBS/$ SEARCH_FOR; then LIBS_DIR = $ PHP_LIBSelse # search default path list AC_MSG_CHECKING ([for libs files in default path]) for I in $ SEARCH_PATH; do if test-r $ I/$ SEARCH_FOR; then LIBS_DIR = $ I # path of the searched lib AC_MSG_RESULT (found in $ I) fi donefi/* verify whether lib exists */if test-z "$ LIBS_DIR"; then AC_MSG_RESULT ([not found]) AC_MSG_ERROR ([Please reinstall the libs distribution]) fi/* Add the include directory of lib during compilation,-I/usr/include */PHP_ADD_INCLUDE ($ LIBS_DIR/include) LIBNAME = curl # lib name LIBSYMBOL = curl_version # a function of lib for PHP_CHECK_LIBRARY to verify lib/* to verify lib */PHP_CHECK_LIBRARY ($ LIBNAME, $ LIBSYMBOL, [PHP_ADD_LIBRARY_WITH_PATH ($ LIBNAME, $ LIBS_DIR/$ PHP_LIBDIR, LIBS_SHARED_LIBADD) # link lib,-llibcurl AC_DEFINE (HAVE_LIBSLIB, 1, [])], [AC_MSG_ERROR ([wrong libs lib version or lib not found])], [-L $ LIBS_DIR/$ PHP_LIBDIR-lm]) PHP_SUBST (LIBS_SHARED_LIBADD)

Traverse the array:
function hello_array_strings($arr) {    if (!is_array($arr)) return NULL;    printf("The array passed contains %d elements ", count($arr));     foreach($arr as $data) {        if (is_string($data)) echo "$data ";    }}

PHP_FUNCTION(hello_array_strings){    zval *arr, **data;    HashTable *arr_hash;    HashPosition pointer;    int array_count;     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &arr) == FAILURE) {        RETURN_NULL();    }     arr_hash = Z_ARRVAL_P(arr);    array_count = zend_hash_num_elements(arr_hash);    php_printf("The array passed contains %d elements ", array_count);     for(zend_hash_internal_pointer_reset_ex(arr_hash, &pointer); zend_hash_get_current_data_ex(arr_hash, (void**) &data, &pointer) == SUCCESS; zend_hash_move_forward_ex(arr_hash, &pointer)) {        if (Z_TYPE_PP(data) == IS_STRING) {             PHPWRITE(Z_STRVAL_PP(data), Z_STRLEN_PP(data));             php_printf(" ");         }    }     RETURN_TRUE;}

Output:
int php_printf(const char *format, ...);int php_write(void *buf, uint size TSRMLS_DC);int PHPWRITE(void *buf, uint size);void php_html_puts(const char *buf, uint size TSRMLS_DC)//print zvalZEND_API int zend_print_zval(zval *expr, int indent);ZEND_API void zend_print_zval_r(zval *expr, int indent TSRMLS_DC);//sprintfPHPAPI int spprintf( char **pbuf, size_t max_len, const char *format, ...);PHPAPI int vspprintf(char **pbuf, size_t max_len, const char *format, va_list ap);//string strcatZEND_API int add_char_to_string(zval *result, const zval *op1, const zval *op2);ZEND_API int add_string_to_string(zval *result, const zval *op1, const zval *op2);ZEND_API int concat_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);ZEND_API void zend_str_tolower(char *str, unsigned int length);ZEND_API char *zend_str_tolower_copy(char *dest, const char *source, unsigned int length);ZEND_API char *zend_str_tolower_dup(const char *source, unsigned int length);

Memory allocation:
emalloc(size_t size);efree(void *ptr);ecalloc(size_t nmemb, size_t size);erealloc(void *ptr, size_t size);estrdup(const char *s);estrndup(const char *s, unsigned int length);

The above section introduces the PHP extension summary, including some content, and hopes to help those who are interested in the PHP Tutorial.

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.