环境
centos+宝塔+php7.2
步骤
一:生成扩展目录 PHP源码扩展目录ext/中包含生成扩展基本目录的工具ext_skel,使用该工具可以很方便的生成扩展基本目录。 宝塔位置为:/www/server/php/72/src/ext/
#cd /www/server/php/72/src/ext/
//my_test_ext 为拓展名字
#./ext_skel --extname=my_test_ext
完成后会自动创建 my_test_ext 文件夹 进入文件夹
cd my_test_ext
二:修改 config.m4 这里dnl 开头为注释的意思
这里先注释16-18行,应该是静态编译需要的参数吧,还不太清楚
dnl $Id$
dnl config.m4 for extension my_test_ext
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_test_ext, for my_test_ext support,
dnl Make sure that the comment is aligned:
dnl [ --with-my_test_ext Include my_test_ext support])
dnl Otherwise use enable:
PHP_ARG_ENABLE(my_test_ext, whether to enable my_test_ext support,
Make sure that the comment is aligned:
[ --enable-my_test_ext Enable my_test_ext support])
if test "$PHP_MY_TEST_EXT" != "no"; then
dnl Write more examples of tests here...
dnl # get library FOO build options from pkg-config output
dnl AC_PATH_PROG(PKG_CONFIG, pkg-config, no)
dnl AC_MSG_CHECKING(for libfoo)
dnl if test -x "$PKG_CONFIG" && $PKG_CONFIG --exists foo; then
dnl if $PKG_CONFIG foo --atleast-version 1.2.3; then
dnl LIBFOO_CFLAGS=`$PKG_CONFIG foo --cflags`
dnl LIBFOO_LIBDIR=`$PKG_CONFIG foo --libs`
dnl LIBFOO_VERSON=`$PKG_CONFIG foo --modversion`
dnl AC_MSG_RESULT(from pkgconfig: version $LIBFOO_VERSON)
dnl else
dnl AC_MSG_ERROR(system libfoo is too old: version 1.2.3 required)
dnl fi
dnl else
dnl AC_MSG_ERROR(pkg-config not found)
dnl fi
dnl PHP_EVAL_LIBLINE($LIBFOO_LIBDIR, MY_TEST_EXT_SHARED_LIBADD)
dnl PHP_EVAL_INCLINE($LIBFOO_CFLAGS)
dnl # --with-my_test_ext -> check with-path
dnl SEARCH_PATH="/usr/local /usr" # you might want to change this
dnl SEARCH_FOR="/include/my_test_ext.h" # you most likely want to change this
dnl if test -r $PHP_MY_TEST_EXT/$SEARCH_FOR; then # path given as parameter
dnl MY_TEST_EXT_DIR=$PHP_MY_TEST_EXT
dnl else # search default path list
dnl AC_MSG_CHECKING([for my_test_ext files in default path])
dnl for i in $SEARCH_PATH ; do
dnl if test -r $i/$SEARCH_FOR; then
dnl MY_TEST_EXT_DIR=$i
dnl AC_MSG_RESULT(found in $i)
dnl fi
dnl done
dnl fi
dnl
dnl if test -z "$MY_TEST_EXT_DIR"; then
dnl AC_MSG_RESULT([not found])
dnl AC_MSG_ERROR([Please reinstall the my_test_ext distribution])
dnl fi
dnl # --with-my_test_ext -> add include path
dnl PHP_ADD_INCLUDE($MY_TEST_EXT_DIR/include)
dnl # --with-my_test_ext -> check for lib and symbol presence
dnl LIBNAME=my_test_ext # you may want to change this
dnl LIBSYMBOL=my_test_ext # you most likely want to change this
dnl PHP_CHECK_LIBRARY($LIBNAME,$LIBSYMBOL,
dnl [
dnl PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $MY_TEST_EXT_DIR/$PHP_LIBDIR, MY_TEST_EXT_SHARED_LIBADD)
dnl AC_DEFINE(HAVE_MY_TEST_EXTLIB,1,[ ])
dnl ],[
dnl AC_MSG_ERROR([wrong my_test_ext lib version or lib not found])
dnl ],[
dnl -L$MY_TEST_EXT_DIR/$PHP_LIBDIR -lm
dnl ])
dnl
dnl PHP_SUBST(MY_TEST_EXT_SHARED_LIBADD)
PHP_NEW_EXTENSION(my_test_ext, my_test_ext.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
fi
这里关键是 最后一行 PHP_NEW_EXTENSION 这个方法 这里会加载你写的C代码,现在默认加载了my_test_ext.c,如果有多个应该这样加
dnl 多个C文件中间用空格键隔开
PHP_NEW_EXTENSION(my_test_ext, my_test_ext.c test_2.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
三:实现自己的拓展方法 暂时只有一个C文件 我们就在默认生成的my_test_ext.c 里面添加自己的方法
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_my_test_ext.h"
/* If you declare any globals in php_my_test_ext.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(my_test_ext)
*/
/* True global resources - no need for thread safety here */
static int le_my_test_ext;
/* {{{ PHP_INI
*/
/* Remove comments and fill if you need to have entries in php.ini
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("my_test_ext.global_value", "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_my_test_ext_globals, my_test_ext_globals)
STD_PHP_INI_ENTRY("my_test_ext.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_my_test_ext_globals, my_test_ext_globals)
PHP_INI_END()
*/
/* }}} */
/* Remove the following function when you have successfully modified config.m4
so that your module can be compiled into PHP, it exists only for testing
purposes. */
/* Every user-visible function in PHP should document itself in the source */
/* {{{ proto string confirm_my_test_ext_compiled(string arg)
Return a string to confirm that the module is compiled in */
// PHP_FUNCTION(confirm_my_test_ext_compiled)
// {
// char *arg = NULL;
// size_t arg_len, len;
// zend_string *strg;
// if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &arg, &arg_len) == FAILURE) {
// return;
// }
// strg = strpprintf(0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "my_test_ext", arg);
// RETURN_STR(strg);
// }
/* }}} */
/* The previous line is meant for vim and emacs, so it can correctly fold and
unfold functions in source code. See the corresponding marks just before
function definition, where the functions purpose is also documented. Please
follow this convention for the convenience of others editing your code.
*/
/* {{{ php_my_test_ext_init_globals
*/
/* Uncomment this function if you have INI entries
static void php_my_test_ext_init_globals(zend_my_test_ext_globals *my_test_ext_globals)
{
my_test_ext_globals->global_value = 0;
my_test_ext_globals->global_string = NULL;
}
*/
/* }}} */
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(my_test_ext)
{
/* If you have INI entries, uncomment these lines
REGISTER_INI_ENTRIES();
*/
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(my_test_ext)
{
/* uncomment this line if you have INI entries
UNREGISTER_INI_ENTRIES();
*/
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(my_test_ext)
{
#if defined(COMPILE_DL_MY_TEST_EXT) && defined(ZTS)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(my_test_ext)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(my_test_ext)
{
php_info_print_table_start();
php_info_print_table_header(2, "my_test_ext support", "enabled");
php_info_print_table_end();
/* Remove comments if you have entries in php.ini
DISPLAY_INI_ENTRIES();
*/
}
//定义拓展方法
ZEND_FUNCTION(sey_hello)
{
php_printf("Hello World!\n");
}
/* }}} */
/* {{{ my_test_ext_functions[]
*这里需要添加自己拓展的方法
* Every user visible function must have an entry in my_test_ext_functions[].
*/
const zend_function_entry my_test_ext_functions[] = {
// PHP_FE(confirm_my_test_ext_compiled, NULL) /* For testing, remove later. */
PHP_FE(tl_get_arch, NULL)
PHP_FE_END /* Must be the last line in my_test_ext_functions[] */
};
/* }}} */
/* {{{ my_test_ext_module_entry
*/
zend_module_entry my_test_ext_module_entry = {
STANDARD_MODULE_HEADER,
"my_test_ext",
my_test_ext_functions,
PHP_MINIT(my_test_ext),
PHP_MSHUTDOWN(my_test_ext),
PHP_RINIT(my_test_ext), /* Replace with NULL if there's nothing to do at request start */
PHP_RSHUTDOWN(my_test_ext), /* Replace with NULL if there's nothing to do at request end */
PHP_MINFO(my_test_ext),
PHP_MY_TEST_EXT_VERSION,
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_MY_TEST_EXT
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
#endif
ZEND_GET_MODULE(my_test_ext)
#endif
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/
里面125 行添加了自定义的方法 say_hello
四:把方法添加到PHP拓展调用层 my_test_ext.c 里修改
const zend_function_entry my_test_ext_functions[] = {
// PHP_FE(confirm_my_test_ext_compiled, NULL) /* For testing, remove later. */
PHP_FE(say_hello, NULL)
PHP_FE_END /* Must be the last line in my_test_ext_functions[] */
};
五:在php_my_test_ext.h 里添加方法声明
#ifndef PHP_MY_TEST_EXT_H
#define PHP_MY_TEST_EXT_H
extern zend_module_entry my_test_ext_module_entry;
#define phpext_my_test_ext_ptr &my_test_ext_module_entry
#define PHP_MY_TEST_EXT_VERSION "0.1.0" /* Replace with version number for your extension */
#ifdef PHP_WIN32
# define PHP_MY_TEST_EXT_API __declspec(dllexport)
#elif defined(__GNUC__) && __GNUC__ >= 4
# define PHP_MY_TEST_EXT_API __attribute__ ((visibility("default")))
#else
# define PHP_MY_TEST_EXT_API
#endif
#ifdef ZTS
#include "TSRM.h"
#endif
/*
Declare any global variables you may need between the BEGIN
and END macros here:
ZEND_BEGIN_MODULE_GLOBALS(my_test_ext)
zend_long global_value;
char *global_string;
ZEND_END_MODULE_GLOBALS(my_test_ext)
*/
/* Always refer to the globals in your function as MY_TEST_EXT_G(variable).
You are encouraged to rename these macros something shorter, see
examples in any other php module directory.
*/
#define MY_TEST_EXT_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(my_test_ext, v)
#if defined(ZTS) && defined(COMPILE_DL_MY_TEST_EXT)
ZEND_TSRMLS_CACHE_EXTERN()
#endif
PHP_FUNCTION(say_hello);
#endif /* PHP_MY_TEST_EXT_H */
倒数第二行 添加的方法声明
六:编译拓展 拓展文件内
#make clean
#/www/server/php/72/bin/phpize
#./configure --with-php-config=/www/server/php/72/bin/php-config
#make && make install
逐条执行完上面的指令则编译好自定义拓展了 最后在php.ini 加上拓展 重启php 则可以调用了 extension= /www/server/php/72/lib/php/extensions/no-debug-non-zts-20170718/my_test_ext.so
最后 php调用
//测试自己的拓展
public function test(){
say_hello();
}
参考资料:php内核开发 https://github.com/walu/phpbook
|