系统环境
Centos 7 x86_64
PHP 编译与安装
$ cd /home/www/tools
$ wget https://www.php.net/distributions/php-7.4.28.tar.gz
$ tar -xzvf ./php-7.4.28.tar.gz
$ cd php-7.4.28
$ pwd
/home/www/tools/php-7.4.28
$ yum install -y gcc libxml2 libxml2-devel sqlite-devel
$ ./configure --prefix=/home/www/install/php7.4.28--enable-fpm --enable-debug
$ make
$ make install
$ yum install -y tree
$ tree -L 3 /home/www/install/
/home/www/install/ ├── bin │ ├── phar -> phar.phar │ ├── phar.phar │ ├── php │ ├── php-cgi │ ├── php-config │ ├── phpdbg │ └── phpize ├── etc │ ├── php-fpm.conf.default │ └── php-fpm.d │ └── www.conf.default ├── include │ └── php │ ├── ext │ ├── include │ ├── main │ ├── sapi │ ├── TSRM │ └── Zend ├── lib │ └── php │ ├── build │ └── extensions ├── php │ ├── man │ │ ├── man1 │ │ └── man8 │ └── php │ └── fpm ├── sbin │ └── php-fpm └── var ├── log └── run 25 directories, 10 files
$ echo 'PATH=$PATH:/home/www/install/php7.4.28/bin' >> /etc/profile
$ source /etc/profile
$ php -v
PHP 7.4.28 (cli) (built: Feb 20 2022 08:26:53) ( NTS DEBUG )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
$ cp /home/www/tools/php-7.4.28/php.ini-development /home/www/install/php7.4.28/lib/php.ini
$ php --ini
...
Loaded Configuration File: /home/www/install/php7.4.28/lib/php.ini
...
扩展开发
$ cd /home/www/tools/php-7.4.28/ext
$ php ./ext_skel.php --ext mydemoext
$ cd mydemoext
$ tree
. ├── config.m4 ├── config.w32 ├── mydemoext.c ├── php_mydemoext.h └── tests ├── 001.phpt ├── 002.phpt └── 003.phpt 1 directory, 7 files
$ vim mydemoext.c
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "php.h"
#include "ext/standard/info.h"
#include "php_mydemoext.h"
#ifndef ZEND_PARSE_PARAMETERS_NONE
#define ZEND_PARSE_PARAMETERS_NONE() \
ZEND_PARSE_PARAMETERS_START(0, 0) \
ZEND_PARSE_PARAMETERS_END()
#endif
PHP_FUNCTION(hello)
{
char *var;
size_t var_len;
zend_string *retval;
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_STRING(var, var_len)
ZEND_PARSE_PARAMETERS_END();
retval = strpprintf(0, "Hello %s", var);
RETURN_STR(retval);
}
PHP_RINIT_FUNCTION(mydemoext)
{
#if defined(ZTS) && defined(COMPILE_DL_MYDEMOEXT)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
return SUCCESS;
}
PHP_MINFO_FUNCTION(mydemoext)
{
php_info_print_table_start();
php_info_print_table_header(2, "mydemoext support", "enabled");
php_info_print_table_end();
}
ZEND_BEGIN_ARG_INFO(arginfo_hello, 0)
ZEND_ARG_INFO(0, str)
ZEND_END_ARG_INFO()
static const zend_function_entry mydemoext_functions[] = {
PHP_FE(hello, arginfo_hello)
PHP_FE_END
};
zend_module_entry mydemoext_module_entry = {
STANDARD_MODULE_HEADER,
"mydemoext",
mydemoext_functions,
NULL,
NULL,
PHP_RINIT(mydemoext),
NULL,
PHP_MINFO(mydemoext),
PHP_MYDEMOEXT_VERSION,
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_MYDEMOEXT
# ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
# endif
ZEND_GET_MODULE(mydemoext)
#endif
$ yum install -y autoconf
$ /home/www/install/php7.4.28/bin/phpize
$ ./configure --with-php-config=/home/www/install/php7.4.28/bin/php-config
$ make
$ ls modules/
mydemoext.la mydemoext.so
$ make install
Installing shared extensions: /home/www/install/php7.4.28/lib/php/extensions/debug-non-zts-20190902/
$ vim /home/www/install/php7.4.28/lib/php.ini
extension=/home/www/install/php7.4.28/lib/php/extensions/debug-non-zts-20190902/mydemoext.so
$ php -m
[PHP Modules]
...
mydemoext
...
测试
$ vim test.php
<?php
echo hello('xchenhao') . "\n";
$ php test.php
Hello xchenhao
参考
- https://www.zend.com/resources/writing-php-extensions
Writing PHP Extensions
|