Home > Backend Development > PHP Tutorial > Write your own php extension function_PHP tutorial

Write your own php extension function_PHP tutorial

WBOY
Release: 2016-07-21 16:10:01
Original
847 people have browsed it


Write your own php extension function I have been writing PHP programs for a long time, so I naturally know the functions it provides very well. The many functions it provides are really useful, but sometimes I find that PHP lacks some functions, and I always feel like adding some to PHP. Custom feature ideas. As time went by, I finally couldn't hold it any longer today and started researching how to add it.



Download a php source code package. The version used here is php 4.0.5. After decompression, you will see a file like README.EXT_SKEL in the root directory of php. Open it. After reading it in detail, I found a very useful tool. This tool can help you build an empty php extension, and then you can add the corresponding code to it to complete your own function extension. Below 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, I chose my_module. After executing the tool, a directory with the module_name of your choice will be automatically created in the ext directory. Relevant codes have been generated in it. In these codes, you only need to adjust the three lines of comments in the config.m4 file to compile normally with this customization. Extension module for PHP. You can get it by performing the following operations in the root directory of php.

./buildconf

./configure --enable-module_name

make



Now I will demonstrate the establishment of my_module extension framework The whole process, in order to be more effective, let's complete a PHP extension function. Calling this function in PHP can display the classic word "hello world" in the web page.

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

./ext_skel --extname=my_module

Get the feedback result:

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, then do it. If you don’t understand it very well, you can just follow my tips below.

Cd my_module

首先进入my_module目录

vi config.m4

使用文本编辑器打开config.m4文件,文件内容大致如下:

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

  

根据你自己的选择将

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])

修改成

PHP_ARG_WITH(my_module, for my_module support,

Make sure that the comment is aligned:

[  --with-my_module             Include my_module support])

或者将

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])

修改成

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])

  

一般我会选择后者,然后保存退出。如果你对vi文本编辑器的操作有困难的话,请参考相应的说明文章,这里就不再详细描述了。

Vi my_module.c

将文件其中的下列代码进行修改

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

*/

function_entry my_module_functions[] = {

        PHP_FE(say_hello,       NULL)  /* ß添加着一行代码 */

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

        {NULL, NULL, NULL}      /* Must be the last line in my_module_functions[] */

};

  

在文件的最后添加下列代码

PHP_FUNCTION(say_hello)

{

        zend_printf("hello worldn");

}

保存文件退出

  

vi php_my_module.h

在文件中PHP_FUNCTION(confirm_my_module_compiled);一行前面添加下面的代码

PHP_FUNCTION(say_hello);

保存文件退出

  

退回到php的根目录下,执行下面的命令

./buildconf

./configure --enable-my_module

make

  

如果一切顺利的话,我们现在已经将扩展模块my_module编译到php里面了。我们编写下面的代码进行测试


       Say_hello();

?>

保存文件为say_hello.php

在php的根目录下运行

./php –q say_hello.php

正常情况下会显示

hello world

表示我们的第一个扩展正常的运行了!

  

解释一下上面做的操作,ext_skel生成一些框下文件,我们需要修改以下文件

my_module.c  扩展模块的主程序

php_my_module.h 扩展模块的头文件

config.m4  配置文件

  

主程序中描述了php扩展模块的声明,模块中含有多少个函数,各个函数的作用,在phpinfo函数中显示什么内容,模块初始化做些什么,结束做些什么都会在这个文件里进行描述。We just added a function say_hello above, and described the specific content of the say_hello function, and called the zend_printf system function to print a string in PHP.



declares the say_hello function in the corresponding header file, thereby completing the functions we expected. Next we will write a more complex extension, create a php extension function with parameters, and 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 content 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, %sn", Z_STRVAL_PP(yourname));

}

Save and exit.

Return to the root directory of php, run

make

modify say_hello.php to


Say_hello(“yorgo ”);

?>

Save and exit and run

./php –q say_hello.php

Get the result

hello world, yorgo

means that our modification this time has been successful. We can change the parameters in say_hello to see the dynamic effect.

Here we mainly explain the content of the function modified above. Since the say_hello function requires the introduction of parameters, the say_hello function in my_module.c mainly processes the parameters and fills in the parameter content when referencing say_hello in php. Correctly passed to the say_hello processing function in my_module.c. For this purpose, these few lines have been added to the program.

zval **yourname;

if (ZEND_NUM_ARGS() != 1
zend_get_parameters_ex(1, &yourname) == FAILURE)

{

WRONG_PARAM_COUNT;

}

zend_printf("hello world, %sn", Z_STRVAL_PP(yourname));



The code is explained as follows:

zval **yourname;

Initialize a parameter pointer

ZEND_NUM_ARGS()

Get the number of parameters passed, and determine if it is not 1 Indicates there is a problem and reports an error.

zend_get_parameters_ex(1, &yourname)

Point the just initialized pointer to the passed parameters, and report an error if unsuccessful.

Z_STRVAL_PP(yourname)

Handle the parameter pointed to by the pointer and get the actual stored value.

(To be continued)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/314349.htmlTechArticleWriting your own php extension functions. I have been writing php programs for a long time, so I naturally know all about the functions he provides. There are a lot of functions that I really find useful, but sometimes I find...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template