[Turn an older article] write your own PHP extension function

Source: Internet
Author: User
Tags add exit end functions header include php and string
Comparison | function Author: floating in the Quartet
I have not yet tested, interested can be tested under

Yorgo Sun 2002/01/22



The PHP program has been written for a long time, and naturally knows the functions he provides, and he offers a lot of features that really feel good, but sometimes it turns out that PHP lacks some functionality and always produces ideas for adding some custom functionality to PHP. Over time, finally today can not hold up, began to study how to add.



Download a PHP source code package, here is the use of PHP 4.0.5 version, decompression will see the root directory of PHP will have readme.ext_skel such a file, open a detailed reading, found a very useful tool, this tool can help you build an empty PHP extension, Then you add the corresponding code to the inside to complete your own extension of the function. Here's how to use this tool.



First transfer your directory to the PHP directory under the EXT 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 that you can choose from, such as the my_module I chose. After executing the tool will automatically in the EXT directory to create your choice of module_name name directory, which has generated the relevant code, which only need to adjust the Config.m4 file in the three lines of comments can normally compile with this custom extension module PHP. You can get it by doing the following in the root directory of PHP.

./buildconf

./configure--enable-module_name

Make



Let me show you the whole process of building the My_module extension framework, and in order to be more effective, we're going to do a PHP extension that calls this feature in PHP to display the word Hello World in a Web page.

In the Ext directory under the PHP directory, execute the following command

./ext_skel--extname=my_module

Get feedback results:

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'll 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 the Your module is compiled into PHP. Then, start writing

Code and repeat the last two steps as often as necessary.



If you can read the above things, do it accordingly. If you are not too clear, follow my instructions below.

Cd My_module

First enter the My_module directory

VI CONFIG.M4

Using a text editor to open the Config.m4 file, the contents of the file are 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 is not work

DNL without editing.



DNL If your extension references something external, use with:



DNL Php_arg_with (My_module, for my_module support,

DNL Make sure this 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 this comment is aligned:

DNL [--enable-my_module enable my_module support])



if test "$PHP _my_module"!= "no"; Then

DNL If You are testing anything external, like existence of

DNL headers, libraries or functions in them, just uncomment the

Dnl following line and your are ready to go.

DNL Write More examples of tests ...

Php_extension (My_module, $ext _shared)

Fi



According to your own choice, you will

DNL Php_arg_with (My_module, for my_module support,

DNL Make sure this comment is aligned:

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

Modified into

Php_arg_with (My_module, for my_module support,

Make sure this comment is aligned:

[--with-my_module Include my_module support])

or to

DNL php_arg_enable (My_module, whether to ENABLE my_module support,

DNL Make sure this comment is aligned:

DNL [--enable-my_module enable my_module support])

Modified into

Php_arg_enable (My_module, whether to ENABLE my_module support,

Make sure this comment is aligned:

[--enable-my_module enable my_module support])



Generally I will choose the latter and then save the exit. If you have difficulty in the operation of the vi text editor, please refer to the corresponding explanatory article, this is no longer described in detail.

Vi my_module.c

Modify the following code in the file

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

*/

Function_entry my_module_functions[] = {

Php_fe (Say_hello, NULL)/*ß adds a line of code * *

Php_fe (confirm_my_module_compiled, NULL)/* For testing, remove later. */

{null, NULL, NULL}/* must is 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 File Exit



VI php_my_module.h

Php_function (confirm_my_module_compiled) in a file, add the following code before a line

Php_function (Say_hello);

Save File Exit



Return to the root directory of PHP and execute the following command

./buildconf

./configure--enable-my_module

Make



If all goes well, we have now compiled the extension module my_module into PHP. We write the following code to test

?

Say_hello ();

?>

Save File as say_hello.php

Running in the root directory of PHP

./php–q say_hello.php

is normally displayed

Hello World

It means our first extension is working!



Explain the operation above, Ext_skel generate some boxes of files, we need to modify the following file

MY_MODULE.C Extension Module Main program

Php_my_module.h the header file of the extension module

CONFIG.M4 configuration file



The main program describes the statement of the PHP extension module, the module contains a number of functions, the functions of each function, what to display in the Phpinfo function, module initialization to do something, the end of what will be described in this file. We just added a function Say_hello, and described the specific contents of the Say_hello function, calling zend_printf system function to print a string in PHP.



The Say_hello function is declared in the corresponding header file, which completes the function we expect. Below we'll write a more complex extension that creates a php extension function with parameters, according to the parameters given, to show Hello World, xxxx. XXXX represents the string content of the input, such as my name Yorgo.



Vi my_module.c

Modify the last Say_hello function 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 exit.

Return to the root directory of PHP, run

Make

Modify say_hello.php to

?

Say_hello ("Yorgo");

?>

Run after save exit

./php–q say_hello.php

Draw the result

Hello World, Yorgo

We have also succeeded in changing the parameters in Say_hello to see the dynamic effect.

This mainly explains the modified function content, because the Say_hello function needs to have the parameter introduction, therefore in MY_MODULE.C's Say_hello function mainly in carries on the parameter processing, the PHP reference Say_hello when fills in the parameter content correctly passes to the My_ In the Say_hello handler function in module.c. To do this, you add a few lines to your program.

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;

Initializes a pointer to a parameter

Zend_num_args ()

Get the number of parameters passed over, and judge if there is a problem when not 1, the error.

ZEND_GET_PARAMETERS_EX (1, &yourname)

The pointer that is just initialized points to the passed parameter, and if it is unsuccessful, the error occurs.

Z_STRVAL_PP (yourname)

Handles the parameters pointed to by the pointer and obtains the actual stored value.

adjourned


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.