PHP extension development (I) DEMO

Source: Internet
Author: User
PHP extension development (I) DEMO
# Getting started 1. download the PHP source code (my version is 5.6.13) 2. Compile and install the code regularly [see my previous blog] ( http://my.oschina.net/lwl1989/blog/511295 ) 3. use ext_skel "cd phpsrc/ext. /ext_skel -- extname = name // name indicates the name of the extension you want to write. below we are all t2 "prompt" 1. $ cd .. 2. $ vi ext/t2/config. m43. $. /buildconf4. $. /configure -- [with | enable]-t25. $ make6. $. /sapi/cli/php-f ext/t2/t2.php7. $ vi ext/t2/t2.c8. $ make "follow the prompts to perform operations. in the second step, we should modify config. lines 10, 11, and 12 of m4 remove dnl [I understand ZEND as a scheme for commenting] at this time, we can compile and install "/usr/local/php/bin/phpize // execute phpize. /configure -- with-php-config =/ Usr/local/php/bin/php-configmake make install "in php. enable the t2.so file in ini. your module will be loaded. check php-m. you can find that the t2 module has been loaded # encoding (hello world). but currently, this extension has no function. So we need to add new features. At this time, we can write the t2.c File (of course, you can also write it in another file, include in t2.c) "PHP_FUNCTION (t2_hello) {printf ("hello world \ n");} "after re-compiling, run php-r" t2_hello (); ". why? Because we did not load t2_hello to the function list prototype: "typedef struct _ zend_function_entry {char * fname; void (* handler) (handler); unsigned char * func_arg_types;} zend_function_entry; "> The parameter description fname is provided to the function name called in PHP. For example, mysql_connecthandler is responsible for processing the pointer of this interface function. Func_arg_types indicates the parameter. It can be set to NULL. we add a line "static const zend_function_entry t2_functions [] = {// Add {NULL, NULL, NULL}" PHP_FE (t2_hello, NULL)> Note: The last item of zend_function_entry must be {NULL, NULL, NULL}. the Zend Engine determines whether the function list has been exported. Then re-compile and run php-r "t2_hello ();" At this moment, our hello world! There is not much time to complete this. next time we will talk about the processing of variable and parameter value transfer. There are several magic methods (zend_module_entry) in the module ).

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.