Compile your own php extension Functions

Source: Internet
Author: User


I have been writing my php Extension function for a long time. Naturally, I am familiar with the functions provided by php. The many functions provided by php are really useful, however, sometimes you may find that php also lacks some features, and you will always have the idea of adding some custom features to php. Over time, I finally got stuck today and started to study how to add it.



Download a php source code package. php 4.0.5 is used here. After decompression, you will see README under the php root directory. an EXT_SKEL file. After reading it in detail, I found a very useful tool that can help you build an empty php extension, then you can add the corresponding code to it to complete your function expansion. Next we will introduce how to use this tool.



First, transfer your directory to the ext directory under the php Directory. If you only need a basic extension framework, execute the following command:

./Ext_skel -- extname = module_name

Module_name is the name of the extension module you can choose, for example, my_module. After the tool is executed, the directory named module_name of your choice is automatically created under the ext Directory, which generates the relevant code. In the code, you only need to adjust the config. the three lines of comments in the m4 file can be used to compile php with the custom extension module normally. In the php root directory, perform the following operations.

./Buildconf

./Configure -- enable-module_name

Make



Next, I will demonstrate the whole process of establishing the my_module extension framework. In order to be more effective, we will complete a php Extension function, you can call this function in php to display the classic word hello world on the web page.

In the ext directory under the php Directory, execute the following command

./Ext_skel -- extname = my_module

Feedback:

Creating directory my_module

Creating basic files: config. m4 Makefile. in. cvsignore my_module.c php_my_module.h tests/001. phpt my_module.php [done].



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

1. $ cd ..

2. $ vi ext/my_module/config. m4

3. $./buildconf

4. $./configure -- [with | enable]-my_module

5. $ make

6. $./php-f ext/my_module/my_module.php

7. $ vi ext/my_module/my_module.c

8. $ make



Repeat steps 3-6 until you are satisfied with ext/my_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.



If you can understand the above, follow the instructions. If not, follow the instructions below.

Cd my_module

First, go to the my_module directory.

Vi config. m4

Open the config. m4 file in the text editor. The file content is roughly as follows:

Dnl $ Id $

Dnl config. m4 for extension my_module

Dnl don't forget to call PHP_EXTENSION (my_module)



Dnl Comments in this file start with the string 'dnl '.

Dnl Remove where necessary. This file will not work

Dnl without editing.



Dnl If your extension references something external, use:



Dnl PHP_ARG_WITH (my_module, for my_module support,

Dnl Make sure that the comment is aligned:

Dnl [-- with-my_module Include my_module support])



Dnl Otherwise use enable:



Dnl PHP_ARG_ENABLE (my_module, whether to enable my_module support,

Dnl Make sure that the comment is aligned:

Dnl [-enable-my_module Enable my_module support])



If test "$ PHP_MY_MODULE "! = "No"; then

Dnl If you will not be testing anything external, like existence

Dnl headers, libraries or functions in them, just uncomment

Dnl following line and you are ready to go.

Dnl Write more examples of tests here...

PHP_EXTENSION (my_module, $ ext_shared)

Fi



Choose

Dnl PHP_ARG_WITH (my_module, for my_module support,

Dnl Make sure that the comment is aligned:

Dnl [-- with-my_module Include my_module support])

Modify

PHP_ARG_WITH (my_module, for my_module support,

Make sure that the comment is aligned:

[-- With-my_module Include my_module support])

Or set

Dnl PHP_ARG_ENABLE (my_module, whether to enable my_module support,

Dnl Make sure that the comment is aligned:

Dnl [-enable-my_module Enable my_module support])

Modify

PHP_ARG_ENABLE (my_module, whether to enable my_module support,

Make sure that the comment is aligned:

[-Enable-my_module Enable my_module support])



Generally, I will select the latter and save and exit. If you have difficulties in operations on the vi text editor, please refer to the relevant instructions and I will not describe them in detail here.

Vi my_module.c

Modify the following code in the file.

/* Every user visible function must have an entry in my_module_functions [].

*/

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 the following code at the end of the file

PHP_FUNCTION (say_hello)

{

Zend_printf ("hello world \ n ");

}

Save the file and exit



Vi php_my_module.h

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



Return to the php root directory and run the following command:

./Buildconf

./Configure -- enable-my_module

Make



If everything goes well, we have now compiled the extension module my_module into php. Let's write the following code for testing.

<?

Say_hello ();

?>

Save the file as say_hello.php.

Run in the php root directory

./Php-q say_hello.php

Normally

Hello world

The first extension is running normally!



To explain the above operations, ext_skel generates some files in the box. We need to modify the following files:

Main Program of my_module.c extension module

Header file of php_my_module.h extension module

Config. m4 configuration file



The main program describes the declaration of the php extension module, the number of functions contained in the module, the role of each function, the content displayed in the phpinfo function, and the initialization of the module, this file will describe what you want to end. We just added a function say_hello and described the specific content of the say_hello function. We called the zend_printf system function to print the string in php.



In the corresponding header file, declare the say_hello function to complete our expected functions. Next we will compile a more complex extension, create a php Extension function with parameters, and display hello world, xxxx according to the input parameters. Xxxx indicates the input string content, for example, my name is yorgo.



Vi my_module.c

The last modified say_hello function is as follows:

PHP_FUNCTION (say_hello)

{

Zval ** yourname;



If (ZEND_NUM_ARGS ()! = 1
Zend_get_parameters_ex (1, & yourname) = FAILURE)

{

WRONG_PARAM_COUNT;

}



Zend_printf ("hello world, % s \ n", Z_STRVAL_PP (yourname ));

}

Save the disk and exit.

Return to the php root directory and run

Make

Modify say_hello.php

<?

Say_hello ("yorgo ");

?>

Save and exit before running

./Php-q say_hello.php

Result

Hello world, yorgo

This modification is successful. You can change the parameters in say_hello to see the dynamic effect.

Here we mainly explain the content of the modified function. Because the say_hello function requires parameter introduction, the say_hello function in my_module.c is mainly processing parameters, pass the parameter content entered when say_hello is referenced in php to the say_hello handler function in my_module.c. To this end, the program adds these lines.

Zval ** yourname;

If (ZEND_NUM_ARGS ()! = 1
Zend_get_parameters_ex (1, & yourname) = FAILURE)

{

WRONG_PARAM_COUNT;

}

Zend_printf ("hello world, % s \ n", Z_STRVAL_PP (yourname ));



The code is explained as follows:

Zval ** yourname;

Initialize a parameter pointer

ZEND_NUM_ARGS ()

Obtain the number of passed parameters. If the value is not 1, an error is returned.

Zend_get_parameters_ex (1, & yourname)

Point the initialized pointer to the passed parameter. If the pointer fails, an error is returned.

Z_STRVAL_PP (yourname)

Process the pointer to the parameter and obtain the actually stored value.

(To be continued)

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.