Php extension module development in Linux

Source: Internet
Author: User
In Linux, php extension module development-general Linux technology-Linux programming and kernel information. The following is a detailed description. The project has been around for nearly half a year. With regard to php extension module development, I have gone through a lot of puzzles since I had no idea how to search and find information and how to use it flexibly, I will record the article here, hoping to help people who have the same needs.

1. Generate a simple php extension

We need two directories: php src and php bin. Download a php source package from the Internet, decompress the package, and install the package.

Php decompress the directory as phpsrc (such as:/home/src/php-4.4.4), installation directory as phpbin (such as/usr/local/php)
Input in shell (I will start with # When there is a shell in the future, no other statement)

# Cd phpsrc/ext
#./Ext_skel -- extname = test_module

Creating directory test_module
Creating basic files: config. m4. cvsignore test_module.c php_test_module.h credits experimental tests/001. phpt test_module.php [done].

To use your new extension, you will have to execute the following steps:

1. $ cd ..
2. $ vi ext/test_module/config. m4
3. $./buildconf
4. $./configure -- [with | enable]-test_module
5. $ make
6. $./php-f ext/test_module/test_module.php
7. $ vi ext/test_module/test_module.c
8. $ make

Repeat steps 3-6 until you are satisfied with ext/test_module/config. m4 and
Step 6 confirms that your module is compiled into PHP. Then, start writing
Code and repeat the last two steps as often as necessary.

The system automatically generates the test_module folder. Next we need to modify several files: config. m4, test_module.c, php_test_module.h, as shown below:

1) Modify config. m4

# Cd test_module
# Vi config. m4

Find the rows

Dnl PHP_ARG_ENABLE (test_module, whether to enable test_module support,

Dnl Make sure that the comment is aligned:
Dnl [-- enable-test_module Enable test_module support])
Remove the dnl from these rows and change it
PHP_ARG_ENABLE (test_module, whether to enable test_module support,

Make sure that the comment is aligned:
[-- Enable-test_module Enable test_module support])

So after the php compilation,./configure plus-enable-test_module can load your php module!

(Step 2) You can do or not do this. You can skip to step 2 to run it .)

2) Modify test_module.c and output what you want.

# Vi test_module.c


Find this section
PHP_FUNCTION (confirm_test_module_compiled)
{
Char * arg = NULL;
Int arg_len, len;
Char string [256];

If (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "s", & arg, & arg_len) = FAILURE ){
Return;
}

Len = sprintf (string, "Congratulations! You have successfully modified ext/%. 78 s/config. m4. Module %. 78 s is now compiled into PHP. "," test_module ", arg );
RETURN_STRINGL (string, len, 1 );
}
Changed:
PHP_FUNCTION (confirm_test_module_compiled)
{
Zend_printf ("This is a test module! ");
}

3) Compile the link

# Cd phpsrc/ext
# Cc-fpic-DCOMPILE_DL_TEST_MODULE = 1-I/usr/local/include-I. -I .. /main-I .. -I .. /TSRM-I .. /Zend-c-o test_module/test_module.o test_module/test_module.c

After the execution, a test_module.o file is generated in the directory, and then connected:

# Cc-shared-L/usr/local/lib-rdynamic-o test_module/test_module.so test_module/test_module.o

4) test:

There are two ways to test whether your extension module is correct. One is to run test in the phpbin directory. php. The second is to run test on the web browser. php (if you have installed apache and other web servers ).

The phpbin test is used here, and no web server is required.

Copy test_module.so to the corresponding directory of phpbin.

(If you do not know which directory it is or do not copy it to the correct location, the error message will indicate the path in which it should be located .)
# Mkdir-p phpbin/lib/php/extensions/(in fact, this directory shows how you set extension_dir in php. ini)
# Cp test_module/test_module.so phpbin/lib/php/extensions/

Create a test. php file in the phpbin directory and write


Dl ("test_module.so ");

// Call a function

Test_module ();

?>

Run in the phpbin directory
Run./php? Q test. php:
This is a test module!

The test is successful. Next we can add our own functions to the extension module to implement our own functions.


2. Add a function to the extension module

To add a function to the php extension module, you only need to modify test_module.c and php_test_module.h.

For example, to add a function sayhello () to the extension module, we need to do the following:

1) Modify php_test_module.h to add a function declaration.
In the file PHP_FUNCTION (confirm_my_module_compiled); Add the following code in front of a line
PHP_FUNCTION (say_hello );
Save the file and exit

2) Modify test_module.c

Add the function entry to function_entry:

Function_entry my_module_functions [] = {
PHP_FE (say_hello, NULL)/* add a line of code to the handler */
PHP_FE (confirm_my_module_compiled, NULL)/* For testing, remove later .*/
{NULL, NULL, NULL}/* Must be the last line in my_module_functions [] */
};

Add functions at the end of the file:
PHP_FUNCTION (say_hello)
{
Zend_printf ("hello world \ n ");
}
Save the file and exit

3) recompile the link to generate a new. so file.

# Cd phpsrc/ext
# Cc-fpic-DCOMPILE_DL_TEST_MODULE = 1-I/usr/local/include-I. -I .. /main-I .. -I .. /TSRM-I .. /Zend-c-o test_module/test_module.o test_module/test_module.c

After the execution, a test_module.o file is generated in the directory, and then connected:

# Cc-shared-L/usr/local/lib-rdynamic-o test_module/test_module.so test_module/test_module.o


Iii. Call third-party C/C ++ Libraries

The third-party C/C ++ library is called in the PHP extension module, which is the same as the C/C ++ library called by common C/C ++ programs, you only need to place the location of the library file in the correct way. The following is an example:

1. Call dynamic library a. so

The required files, a. so, a. h (a. h is mainly used for data type declaration and entry function declaration) are all stored in the test_module directory.

Call a. so:

1) Add a. h at the beginning of test_module.c:

# Include "php. h

# Include "php_ini.h"
# Include "a. h"
After the header file is added, you can call the function interface of a. so in the function of test_module.c.

2) recompile the link and add a. so to the link:

# Cd phpsrc/ext
# Cc-fpic-DCOMPILE_DL_TEST_MODULE = 1-I/usr/local/include-I. -I .. /main-I .. -I .. /TSRM-I .. /Zend-c-o test_module/test_module.o test_module/test_module.c

After the execution, a test_module.o file is generated in the directory, and then connected:

# Cc-shared-L/usr/local/lib-rdynamic-o test_module/test_module.so test_module/test_module.o test_module/a. so

[If the internal implementation of a. so is C ++, should the parameter be added to the link? Lstdc ++, that is:

# Cc? Shared? Lstdc ++-L/usr/local/lib-rdynamic-o test_module/test_module.so test_module/test_module.o test_module/a. so

]

3) When test_module.so is tested, copy both test_module.so and a. so to phpbin/lib/php/extensions.

2. Call static library a.

The required files, a. so, a. h (a. h is mainly used for data type declaration and entry function declaration) are all stored in the test_module directory.

Call a.:

1) and 2) are the same as calling a. so.

3) When test_module.so is tested, copy test_module.so to phpbin/lib/php/extensions/, which is not required by a..

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.