Write your own PHP extension function

zhaozj2021-02-11  204

Write your own PHP extension function

Yorgo Sun 2002/01/22

The PHP program has a long time, naturally, the features he provide, if he provides a lot of functions, it is very easy to use, but sometimes PHP is also lacking, and it will always be added to PHP. Custom features ideas. Over time, I can't hold it today, I started to study how to add it.

Download a PHP source code package, here is the PHP version 4.0.5, after decompression, you will see a file like Readme.ext_skel in the root of PHP, open a detailed reading, found a very easy to use Tools, this tool can help you build an empty PHP extension, then you can add your own features to your own feature to expand it. Let's take a look at how to use this tool.

First transfer your directory to the ext directory in the PHP directory. If you only need a basic extension framework, do the following command:

./ext_skel --extName = module_name

Module_name is the name of the extension module you can choose, for example I choose MY_MODULE. After performing the tool, you will automatically build the directory of the module_name name you selected, which has generated the relevant code, which only needs to adjust the three lines of comments in the config.m4 file, which can be adjusted. The PHP of the expansion module. You can get the following operations in the root directory of the PHP.

./buildconf

./configure --enable-module_name

Make

Let me show the whole process of establishing the MY_MODULE extension framework. In order to make more effect, we will complete a PHP extension, call this feature in the PHP to display the Hello World this classic word in the web page.

In the ext directory under the PHP directory, perform the following command

./ext_skel --extName = my_module

Get the feedback results:

CREANG 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 next.

If you can understand the things above, then do it. If you don't understand too much, you can do it according to the prompts below.

CD MY_MODULE

First enter my_module directory

Vi config.m4

Use the text editor to open the config.m4 file, the file content is 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 with:

DNL PHP_ARG_WITH (my_module, for my_module support,

DNL Make Sure That The Comment IS Aligned:

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

DNL OtherWise Uses:

DNL PHP_ARG_ENABLE (MY_MODULE, WHETHER TO ENABLE MY_MODULE SUPPORT,

DNL Make Sure That The Comment IS Aligned:

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

If Test "$ PHP_MY_MODULE! =" no "; then

DNL if you will not be testing anything external, Like EXISTENCE OF EXTERNAL, LIKE EXISTENCE OF

DNL Headers, Libraries Or Functions in Them, Just UNComment THE

DNL FOLLOWING LINE AND You Are Ready to Go.

DNL WRITE more Examples of Tests Here ...

PHP_EXTENSION (MY_MODULE, $ ext_shared)

Fi

According to your own choice

DNL PHP_ARG_WITH (my_module, for my_module support,

DNL Make Sure That The Comment IS Aligned:

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

changed to

PHP_ARG_WITH (my_module, for my_module support,

Make Sure That The Comment IS Aligned:

[--WITH-MY_MODULE INCLUDE MY_MODULE SUPPORT])

Or will

DNL PHP_ARG_ENABLE (MY_MODULE, WHETHER TO ENABLE MY_MODULE SUPPORT,

DNL Make Sure That The Comment IS Aligned:

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

changed to

PHP_ARG_ENABLE (MY_MODULE, WHETHER TOEABLE MY_MODULE Support,

Make Sure That The Comment IS Aligned:

[--Nable-my_module enable my_module support])

Generally I will choose the latter, then save exit. If you have difficulty in the operation of the VI text editor, please refer to the appropriate instructions, this is no longer described in detail.

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 * /

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 in the final addition of the file

PHP_Function (Say_hello)

{

Zend_Printf ("Hello World / N");

}

Save file exits

VI PHP_MY_MODULE.H

PHP_FUNCTION (CONFIRM_MY_MODULE_COMPILED) in the file; add the following code in front of one party

PHP_FUNCTION (SAY_HELLO);

Save file exits

After returning to the root directory of PHP, do the following command

./buildconf

./configure --enable-my_module

Make

If everything goes well, we have now compiled the extension module my_module into the PHP. We write the following code for testing

Say_hello ();

?>

Save files for Say_hello.php

Run under the root of PHP

./php -q sign_hello.php

Will display properly

Hello World

Indicates that our first extension is running!

Explain the above operation, ext_skel generates some box files, we need to modify the following files

MY_MODULE.C extension module's main program

PHP_MY_MODULE.H Extended Module's header file

Config.m4 profile

The statement of the PHP extension module is described in the main program. How many functions are included in the module, the functions of each function, what content displayed in the phpinfo function, what is the initialization of the module, and the ends will be described in this file. We just added a function Say_hello, and described the specific content of the SAY_HELLO function, call the Zend_Printf system function to print strings in PHP.

Declare the SAY_HELLO function in the corresponding header file to complete our expected functionality. Below we will write a more complex extension, create a PHP extension function with parameters, display Hello World, XXXX, based on the parameters given. XXXX represents the input string content, such as my name Yorgo.

Vi my_module.c

Modify the final 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));

}

The storage disk exits.

Return to the root directory of PHP, run

Make

Modify Say_Hello.php

Say_hello ("yorgo");

?>

Save and run out

./php -q sign_hello.php

come to conclusion

Hello World, Yorgo said that our modifications have been successful, can change the parameters in Say_hello to see the dynamic effect.

Here mainly explains the contents of the above modifications, because the SAY_HELLO function needs to be introduced, so the SAY_HELLO function in my_module.c is mainly processed by the parameter, and the parameter content filled in the PHP is correctly passed to MY_MODULE. In the SAY_HELLO process function in C. To this end, there are such a few rows in the 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;

Initialize a pointer to a parameter

Zend_Num_Args ()

Get the number of parameters passing, and determine if there is a problem if it is not 1, an error is reported.

Zend_get_Parameters_ex (1, & Yourname)

Pointed a pointer that is initialized to the passing parameter, if it is not successful, an error is reported.

Z_StrVal_pp (YourName)

Process the parameters directed by the pointer and get the actual stored value.

(to be continued)

Welcome online reprint, but please keep the author's copyright statement, if you need to publish it, please contact the author Contact YiGo@163.net http://www.ruisoft.com

转载请注明原文地址:https://www.9cbs.com/read-5249.html

New Post(0)