In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
php extension writing is what, many novices are not very clear about this, in order to help you solve this problem, the following small series will be explained in detail for everyone, there are people who need this to learn, I hope you can gain something.
Why extend with C?
C is statically compiled and executes much more efficiently than PHP code. The same computing code, developed in C, will perform hundreds of times better than PHP. IO operations such as CURL, because the time is mainly on IOWait, C extension has no obvious advantage.
In addition, the C extension is loaded at the start of the process, PHP code can only operate on the data of the Request life cycle, and the C extension can operate on a wider range.
the first step
Download PHP source code, such as PHP-5.4.16. Unzip and go to php-5.4.16\ext directory. Enter./ ext_skel -extname=myext, myext is the name of the extension, and the myext directory is generated after execution.
ext_skel is an official PHP tool for generating skeleton code for PHP extensions.
cd myext。You can see php_myext.h, myext.c, config.m4, etc. config.m4 is the configuration file for the AutoConf tool, which is used to modify various compilation options.
the second step
Modify config.m4 to
dnl PHP_ARG_WITH(myext, for myext support, dnl Make sure that the comment is aligned: dnl [ --with-myext Include myext support])
modified to
PHP_ARG_WITH(myext, for myext support, [ --with-myext Include myext support])
There is also a-enable-myext below, which means compiled into the PHP kernel. with is loaded as a DLL.
the third step
Modify php_myext.h to see PHP_FUNCTION(confirm_myext_compiled); here is the extension function declaration section, you can add PHP_FUNCTION(myext_helloworld); indicates that a myext_helloworld extension function is declared.
Then modify myext.c, which is the implementation part of the extension function.
const zend_function_entry myext_functions[] = { PHP_FE(confirm_myext_compiled, NULL) /* For testing, remove later. */ PHP_FE(myext_helloworld, NULL) PHP_FE_END /* Must be the last line in myext_functions[] */ };
This code registers the function pointer to the Zend engine, adding PHP_FE(myext_helloworld, NULL)(no semicolon).
the fourth step
Add myext_helloworld execution code to the end of myext.c.
PHP_FUNCTION(myext_helloworld) { char *arg = NULL; int arg_len, len; char *strg; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) { return; } php_printf("Hello World!\ n"); RETRUN_TRUE; }
zend_parameter_parameters is used to accept parameters passed by PHP, RETURN_XXX macro is used to return data to PHP.
the fifth step
Run phpize,./in myext directory configure 、make、make install。Then modify php.ini to add extension= myext.so
Run php -r "myext_helloworld ('test ');" and output hello world!
Did reading the above help you? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.