Home  >  Article  >  Backend Development  >  What is php module development? Introduction to simple php module development

What is php module development? Introduction to simple php module development

伊谢尔伦
伊谢尔伦Original
2017-06-22 13:30:214643browse

In some necessary scenarios we have to develop our own local PHP functions to meet some specific needs, and the new functions must exist in the PHP module. The following will introduce the simplest PHP module development: build your own say_hello($arg) function to output hello world: $arg.

The PHP module development introduced in this document is only to the extent of making hello world. I will not introduce too much about why this is done for the time being. I will introduce the subsequent dissection in more detail.

The hello world-level module can be completed through a few simple steps:

Generate module infrastructure

Modify the module code and add the say_hello function

Modify compilationConfiguration file

Generate module shared library

Configure the module to make the module effective

Test module

1. Generate Module Basics

Enter the ext directory under the PHP source code directory.
Execute ./ext_skel ––extname=sayhello (There is something wrong with the “–” encoding here, please do not copy it directly)
Output:

[root@myhost ext]# ./ext_skel –– extname=sayhello

Creating directory sayhello

Creating basic files: config.m4 config.w32 .cvsignore sayhello.c php_sayhello.h CREDITS EXPERIMENTAL tests/001.phpt sayhello.php [done].

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

1. $ cd ..

2. $ vi ext/sayhello/config.m4

3. $ ./buildconf

4. $ ./configure ––[with|enable]-sayhello

5. $ make

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

7. $ vi ext/sayhello/sayhello.c

8. $ make

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

Seeing that the display output indicates that the module infrastructure has been generated, let’s take a look at what files the generated module contains:

-rw-r–r– 1 root root 2103 Apr 9 05:05 config.m4                                                           using using using using using 2 1   through     through   through   through through over over over over over’s’’’‐’ out to ‐‐‐‐‐‐‐‐‐​​​​​​​​​​​​​​​​​​​

#-rw-r–r– 1 root root 8 Apr 9 05:05 CREDITS                                                                                       use   use with                      . Test version information identification

-rw-r–r– 1 root root 2755 Apr 9 05:05 php_sayhello.h //Module definition header file

-rw-r–r– 1 root root 5294 Apr 9 05:05 sayhello.c //Module implementation file

-rw-r–r– 1 root root 508 Apr 9 05:05 sayhello.php //PHP file used to test module loading

drwxr-xr-x 2 root root 4096 Apr 9 05:05 tests                                                                                                                                                                                                 to be tested () function added.

2. Implement the say_hello() function

Open the php_sayhello.h file of the module and add the c function definition prepared for php say_hello():

PHP_FUNCTION(say_hello); //php The source code defines many macros for module opening, which is quite convenient when you are used to it.

After adding it, we will add the specific implementation of PHP_FUNCTION(say_hello) in say_hello.c:

PHP_FUNCTION(say_hello){
char *arg = NULL;
int arg_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) ==FAILURE) {  //获取php代码的输入参数,方式与scanf差不多
return;
}
zend_printf("hello world : %s",arg);
RETURN_STRINGL(arg, arg_len, 1);
}

Now implement the code Also written, we also need to register this function into the php local function. We need to modify sayhello_functions in sayhello.c:

zend_function_entry sayhello_functions[] = {
PHP_FE(confirm_sayhello_compiled,    NULL)        /*
 For testing, remove later. */
PHP_FE(say_hello,    NULL)        //好,现在say_hello函数也注册了
{NULL, NULL, NULL}  
  /* Must be the last line in sayhello_functions[] */
};

3. Modify the compilation configuration file


Open config.m4, Remove the
comment

symbol "dnl" before the following content:

dnl PHP_ARG_ENABLE(sayhello, whether to enable sayhello support,

dnl Make sure that the comment is aligned:

dnl [ --enable-sayhello Enable sayhello support])

dnl PHP_SUBST(SAYHELLO_SHARED_LIBADD)

4. Compile module and generate shared library

I The module generation method of dynamic libraries is used here, which is much faster than static compilation into PHP, and is convenient for

debugging

(the dynamic library is not loaded using PHP's dl() function, as you can see later).

Enter (cd) sayhello module folder, execute php

Installation

phpize under path bin:

[root@myhost sayhello]# /opt/php_server/php/ bin/phpize

Configuring for:

PHP Api Version: 20041225

Zend Module Api No: 20060613

Zend Extension Api No: 220060519

此时为模块编译的configure文件已经生成。继续生成Makefile文件:

[root@myhost sayhello]# ./configure –with-php-config=/opt/php_server/php/bin/php-config

……没问题的话是没有错误的

现在可以编译了:

make

代码没有问题的话不会有错的。

编译完,进行模块安装:

[root@myhost sayhello]# make install
Installing shared extensions:     /opt/php_server/php/lib/php/extensions/no-debug-non-zts-20060613/

显示模块已经安装至/php安装路径/extensions/no-debug-non-zts-20060613/ 路径下了。

5、配置模块,使其被加载

打开你的php.ini文件开始修改吧:

扩展路径设置:

修改extension_dir = “/php安装路径/lib/php/extensions/no-debug-non-zts-20060613″ //看看上述的模块安装路径就知道了

增加模块设置:

[sayhello]
extension=sayhello.so

ok 大功告成,重新启动你的php模块把。

6、测试模块

写以下php测试代码执行:

<?php
$a = say_hello("frank");
echo "<br>";
echo $a;
?>;

打开这个网页后显示:

hello world : frank
frank

成功运行,模块测试通过。

The above is the detailed content of What is php module development? Introduction to simple php module development. For more information, please follow other related articles on the PHP Chinese website!

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