I remember that the first time I wrote a php extension was directly from Baidu. I wrote an extension based on the Internet, but I didn’t know why. Let's look at the basic steps of an extension first, and then explore the principles behind it.
Use the source code tool to automatically generate the extended directory structure
First enter the php source code ext directory and execute the following command
/www/test/php/php/bin/php ext_skel.php --ext helloworld cd helloworld
Modifyconfig.m4
Configuration file, that is, whether the extension you are writing now uses external dependencies, configure the --with-hello option, otherwise configure the --enable-hello
option, and remove the comments according to your needs
dnl If your extension references something external, use 'with': PHP_ARG_WITH([helloworld], [for helloworld support], [AS_HELP_STRING([--with-helloworld], [Include helloworld support])]) dnl Otherwise use 'enable':
Extended function writing
Thenvim helloworld.c
Write extended function code
First look at the module structure definition
zend_module_entry helloworld_module_entry = { STANDARD_MODULE_HEADER, "helloworld", /* Extension name */ helloworld_functions, /* zend_function_entry */ PHP_MINIT(helloworld), /* PHP_MINIT - Module initialization */ NULL, /* PHP_MSHUTDOWN - Module shutdown */ PHP_RINIT(helloworld), /* PHP_RINIT - Request initialization */ NULL, /* PHP_RSHUTDOWN - Request shutdown */ PHP_MINFO(helloworld), /* PHP_MINFO - Module info */ PHP_HELLOWORLD_VERSION, /* Version */ PHP_MODULE_GLOBALS(pib), NULL, NULL, NULL, STANDARD_MODULE_PROPERTIES_EX };
Function function name collection
static const zend_function_entry helloworld_functions[] = { PHP_FE(helloworld_test1, arginfo_helloworld_test1) PHP_FE(helloworld_test2, arginfo_helloworld_test2) PHP_FE_END };
Real function function code
PHP_FUNCTION(helloworld_test2) { int argc = ZEND_NUM_ARGS(); char *messages = NULL; size_t messages_len = 0; char *context = NULL; size_t context_len = 0; zend_string *retval; ZEND_PARSE_PARAMETERS_START(0, 2) Z_PARAM_OPTIONAL Z_PARAM_STRING(messages, messages_len) Z_PARAM_STRING(context, context_len) ZEND_PARSE_PARAMETERS_END(); retval = strpprintf(0, "Hello %s test %s", messages, context); RETURN_STR(retval); }
Function parameter definition
ZEND_BEGIN_ARG_INFO(arginfo_helloworld_test2, 0) ZEND_ARG_INFO(0, str) ZEND_END_ARG_INFO()
Compile and install
/www/test/php/php/bin/phpize ./configure --with-php-config=/www/test/php/php/bin/php-config make && make install
Now that the file helloworld.so
already exists in the PHP extension directory, add the extended configuration <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">extension = helloworld.so</pre><div class="contentsignin">Copy after login</div></div>
to
and then you can test what you have written. functionhelloworld_test2()
;After completing an extension, I feel that I have gained nothing. I don’t know the principle of why I wrote it like this. Let’s talk about the principle in the next part. Let’s start with The beginning of the PHP life cycle is introduced, see Next Part.
Recommended learning: "PHP Video Tutorial"