PHP extension helloword 1. download the source code.
Download the PHP source code from the official website. here I use PHP5.3.5.
Open the ext directory and we can see that the directory contains all extensions. Under the changed Directory, we can see that under the ext_skel script, I used this command to generate the basic architecture of the extension.
II. create a basic framework
./ext_skel --extname=example
This command will generate an example directory under the ext directory.
# There are many files in the directory, testmodule. c and config. m4 is the most important example. c # is the main extension file config. m4 # m4 is a macro interpretation tool used to generate our extended makefileCREDITES # This file does not play a major role, it's just used to append some additional information when releasing your extension EXPERIMENTAL # This file just indicates that this extension is EXPERIMENTAL, so you don't need to worry about it. php_example.h # This is our extended header file tests/001. phpt # This is also a test file, but it uses unit testing and phase testing.
3. implementation.
1. modify the configuration file config. m4 and remove the dnl (note the dnl in the m4 file)
Vi config. examples (example, whether to enable laiwenhui support, [-- enable-laiwenhui Enable laiwenhui support]) # PHP_ARG_WITH (testmodule, for testmodule support, # Make sure that the comment is aligned: # -- with-testmodule Include testmodule support]) # here, with indicates that to enable this module, it depends on some other modules. we can skip this section for the time being. # For example, if the module example depends on apxs, you need to: #/configure -- with-apxs =/usr/local/apache/bin/apxs -- enable-example
2. declare a function
Edit the file in php_example.h.
vi php_example.h
We can find PHP_FUNCTION (confirm_example_compiled); add PHP_FUNCTION (test) after it );
3. implement the function test
vi example.c
Write the following code to the end
/*** The first PHP extension I added ***/PHP_FUNCTION (test) {char * str = NULL; char * arg = NULL; int arg_len; int len; // str = "Hello my first php extention! ''';
//len = strlen(str);
//RETURN_STRINGL(str, len, 0);
// It may be ineffective to start writing like this. I don't know why it doesn't work. Does not support strlen but can be compiled and passed. If (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "s", & arg, & arg_len) = FAILURE) {return;} len = spprintf (& str, 0, "Hello my first php extention! Pai_^ param % s ", arg); RETURN_STRINGL (str, len, 0 );}
Spprintf should be a php function.
Zend_parse_parameters is a PHP function indicating to retrieve parameter resolution. if it fails, return.
4. tell zend Engine which functions are included in this example module.
Find the row zend_function_entry testmodule_functions [], which means that all available functions must be defined here.
To:
* Every user visible function must have an entry in example_functions []. */const zend_function_entry example_functions [] = {PHP_FE (confirm_example_compiled, NULL)/* For testing, remove later. */PHP_FE (test, NULL)/* my extension test */{NULL, NULL, NULL}/* Must be the last line in example_functions [] */}; /*}}}
5. Compile
Enter the example module
./Configure? With-php-config =/usr/local/php/bin/php-config
/Usr/local/php/bin/phpize
Make
Cp modules/example. so/usr/local/php/lib/php/extensions/
Modify php. ini, enable example. so, and restart the Web service.
4. test.
Here is an article about laruence, which gives a clearer explanation.
Http://www.laruence.com/2009/04/28/719.html