在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
PHP 尽管提供了大量有用的函数,但是在特殊情况下还可能需要进行扩展编程,比如大量的 PECL(PHP Extension Community Library)就是以扩展的形式提供的(动态链接库dll文件),它们比 PEAR 的运行效率要高很多。 过程: 1,安装 VC++6.0,并选择把其可执行文件路径加入环境变量中,使在命令行环境任意路径下可以运行编译器。 To use your new extension, you will have to execute the following steps: 1. $ cd .. Repeat steps 3-6 until you are satisfied with ext/myphpext/config.m4 and 结果在 ext 下生成一个文件夹 myphpext,包含一个 PHP 扩展应用编程框架。myphpext 可以任意取名,将来生成的 dll 文件格式为 php_[extname].dll,我们生成的就是 php_myphpext.dll。运行结果的提示信息 1.2...8 主要是对 Linux/Unix 环境而言的,我们不必理会。其实 config.m4 文件在 Windows 下也可能需要修改,但是对于我们简单的框架暂时还用不着。 文件夹 myphpext 包含若干个文件,其中: myphpext.dsp 是工程文件,后边还要用; 以上 2 个重要的文件内容: php_myphpext.h 文件:
/*
+----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2007 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_01.txt | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | [email protected] so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: | +----------------------------------------------------------------------+ */ /* $Id: header,v 1.16.2.1.2.1 2007/01/01 19:32:09 iliaa Exp $ */ #ifndef PHP_MYPHPEXT_H #define PHP_MYPHPEXT_H extern zend_module_entry myphpext_module_entry; #define phpext_myphpext_ptr &myphpext_module_entry #ifdef PHP_WIN32 #define PHP_MYPHPEXT_API __declspec(dllexport) #else #define PHP_MYPHPEXT_API #endif #ifdef ZTS #include "TSRM.h" #endif PHP_MINIT_FUNCTION(myphpext); PHP_MSHUTDOWN_FUNCTION(myphpext); PHP_RINIT_FUNCTION(myphpext); PHP_RSHUTDOWN_FUNCTION(myphpext); PHP_MINFO_FUNCTION(myphpext); PHP_FUNCTION(confirm_myphpext_compiled); /* For testing, remove later. */ PHP_FUNCTION(HelloPHP); /* Declare any global variables you may need between the BEGIN and END macros here: ZEND_BEGIN_MODULE_GLOBALS(myphpext) long global_value; char *global_string; ZEND_END_MODULE_GLOBALS(myphpext) */ /* In every utility function you add that needs to use variables in php_myphpext_globals, call TSRMLS_FETCH(); after declaring other variables used by that function, or better yet, pass in TSRMLS_CC after the last function argument and declare your utility function with TSRMLS_DC after the last declared argument. Always refer to the globals in your function as MYPHPEXT_G(variable). You are encouraged to rename these macros something shorter, see examples in any other php module directory. */ #ifdef ZTS #define MYPHPEXT_G(v) TSRMG(myphpext_globals_id, zend_myphpext_globals *, v) #else #define MYPHPEXT_G(v) (myphpext_globals.v) #endif #endif /* PHP_MYPHPEXT_H */ /* * 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 */ myphpext.c 文件:
+----------------------------------------------------------------------+
| PHP Version 5 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2007 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_01.txt | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | [email protected] so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: | +----------------------------------------------------------------------+ */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "php.h" #include "php_ini.h" #include "ext/standard/info.h" #include "php_myphpext.h" ZEND_DECLARE_MODULE_GLOBALS(myphpext) */ static int le_myphpext; * * Every user visible function must have an entry in myphpext_functions[]. */ zend_function_entry myphpext_functions[] = PHP_FE(confirm_myphpext_compiled, NULL) PHP_FE(HelloPHP, NULL) }; */ zend_module_entry myphpext_module_entry = #if ZEND_MODULE_API_NO >= 20010901 STANDARD_MODULE_HEADER, #endif "myphpext", myphpext_functions, PHP_MINIT(myphpext), PHP_MSHUTDOWN(myphpext), PHP_RINIT(myphpext), PHP_RSHUTDOWN(myphpext), PHP_MINFO(myphpext), #if ZEND_MODULE_API_NO >= 20010901 "0.1", #endif STANDARD_MODULE_PROPERTIES }; #ifdef COMPILE_DL_MYPHPEXT ZEND_GET_MODULE(myphpext) #endif */ PHP_INI_BEGIN() STD_PHP_INI_ENTRY("myphpext.global_value", "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_myphpext_globals, myphpext_globals) STD_PHP_INI_ENTRY("myphpext.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_myphpext_globals, myphpext_globals) PHP_INI_END() */ */ static void php_myphpext_init_globals(zend_myphpext_globals *myphpext_globals) { myphpext_globals->global_value = 0; myphpext_globals->global_string = NULL; } */ */ PHP_MINIT_FUNCTION(myphpext) REGISTER_INI_ENTRIES(); */ return SUCCESS; } */ PHP_MSHUTDOWN_FUNCTION(myphpext) UNREGISTER_INI_ENTRIES(); */ return SUCCESS; } /* Remove if there's nothing to do at request start */ /* {{{ PHP_RINIT_FUNCTION */ PHP_RINIT_FUNCTION(myphpext) { return SUCCESS; } /* }}} */ /* Remove if there's nothing to do at request end */ /* {{{ PHP_RSHUTDOWN_FUNCTION */ PHP_RSHUTDOWN_FUNCTION(myphpext) { return SUCCESS; } /* }}} */ /* {{{ PHP_MINFO_FUNCTION */ PHP_MINFO_FUNCTION(myphpext) { php_info_print_table_start(); php_info_print_table_header(2, "myphpext support", "enabled"); php_info_print_table_end(); /* Remove comments if you have entries in php.ini DISPLAY_INI_ENTRIES(); */ } /* }}} */ /* Remove the following function when you have succesfully 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_myphpext_compiled(string arg) Return a string to confirm that the module is compiled in */ PHP_FUNCTION(confirm_myphpext_compiled) { char *arg = NULL; int arg_len, len; char *strg; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) { return; } len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "myphpext", arg); RETURN_STRINGL(strg, len, 0); } PHP_FUNCTION(HelloPHP) { php_printf("Hello, PHP v5.2.5 - 2008-3-28"); } /* }}} */ /* 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. */ /* * 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 */ 注意本例定义了一个函数 HelloPHP。在 php_myphpext.h 文件中定义: 在 myphpext.c 中有 2 处地方: PHP_FE(HelloPHP, NULL) 语句把我们自己的函数加入入口数组中。 以下定义了 HelloPHP 函数的内容: 其实还有个 confirm_myphpext_compiled 函数,是自动产生的,用于测试,与我们的自定义函数用法一模一样。 5,编译、链接,生成最终的文件。 C:\php-5.2.5-Win32\ext>msdev myphpext\myphpext.dsp /MAKE "myphpext - Win32 Release_TS" php_myphpext.dll - 0 error(s), 0 warning(s) 最终在 C:\php-5.2.5-Win32\Release_TS 下生成了扩展库 php_myphpext.dll。 6,部署: 把 php_myphpext.dll 拷贝到 C:\php-5.2.5-Win32\ext 文件夹下。修改 php.ini 文件: 加语句 extension=php_myphpext.dll。 再注意 extension 路径的指向,需要把 ;extension_dir = "./" 语句的注释去掉,再修改为 extension_dir = "C:\php-5.2.5-Win32\ext"。 最后一定要重启 IIS 服务器。 7,测试: 把 myphpext.php 拷贝到 Web 服务器根下(myphpext.php 的代码也值得一看),在本机用浏览器打开:http://localhost/myphpext.php,应该能看到以下信息: Functions available in the test extension: Congratulations! You have successfully modified ext/myphpext/config.m4. Module myphpext is now compiled into PHP. 再建立一个 test.php 文件,内容为:
<?php
HelloPHP(); ?>
用本机浏览器打开:http://localhost/test.php,应该能看到以下信息: Hello, PHP v5.2.5 - 2008-3-28 这说明我们所有的步骤都是正确的,已经生成了一个自己的 PHP 扩展函数库。只要对 C
语言熟悉,就可以编写大量的自定义函数,供所有人调用。注意,不像 PEAR 等函数库,需要首先在 PHP
代码里指定其文件名才能使用其中的函数和类。PHP 扩展的函数不是用 PHP 语言自身开发的,而是 C 开发的,而且可以直接在 PHP
代码里调用。这样既有一定的保密性,还有效率上的优势。 PHP 尽管提供了大量有用的函数,但是在特殊情况下还可能需要进行扩展编程,比如大量的 PECL(PHP Extension Community Library)就是以扩展的形式提供的(动态链接库dll文件),它们比 PEAR 的运行效率要高很多。 过程: 1,安装 VC++6.0,并选择把其可执行文件路径加入环境变量中,使在命令行环境任意路径下可以运行编译器。 To use your new extension, you will have to execute the following steps: 1. $ cd .. Repeat steps 3-6 until you are satisfied with ext/myphpext/config.m4 and 结果在 ext 下生成一个文件夹 myphpext,包含一个 PHP 扩展应用编程框架。myphpext 可以任意取名,将来生成的 dll 文件格式为 php_[extname].dll,我们生成的就是 php_myphpext.dll。运行结果的提示信息 1.2...8 主要是对 Linux/Unix 环境而言的,我们不必理会。其实 config.m4 文件在 Windows 下也可能需要修改,但是对于我们简单的框架暂时还用不着。 文件夹 myphpext 包含若干个文件,其中: myphpext.dsp 是工程文件,后边还要用; 以上 2 个重要的文件内容: php_myphpext.h 文件:
/*
+----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2007 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_01.txt | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | [email protected] so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: | +----------------------------------------------------------------------+ */ /* $Id: header,v 1.16.2.1.2.1 2007/01/01 19:32:09 iliaa Exp $ */ #ifndef PHP_MYPHPEXT_H #define PHP_MYPHPEXT_H extern zend_module_entry myphpext_module_entry; #define phpext_myphpext_ptr &myphpext_module_entry #ifdef PHP_WIN32 #define PHP_MYPHPEXT_API __declspec(dllexport) #else #define PHP_MYPHPEXT_API #endif #ifdef ZTS #include "TSRM.h" #endif PHP_MINIT_FUNCTION(myphpext); PHP_MSHUTDOWN_FUNCTION(myphpext); PHP_RINIT_FUNCTION(myphpext); PHP_RSHUTDOWN_FUNCTION(myphpext); PHP_MINFO_FUNCTION(myphpext); PHP_FUNCTION(confirm_myphpext_compiled); /* For testing, remove later. */ PHP_FUNCTION(HelloPHP); /* Declare any global variables you may need between the BEGIN and END macros here: ZEND_BEGIN_MODULE_GLOBALS(myphpext) long global_value; char *global_string; ZEND_END_MODULE_GLOBALS(myphpext) */ /* In every utility function you add that needs to use variables in php_myphpext_globals, call TSRMLS_FETCH(); after declaring other variables used by that function, or better yet, pass in TSRMLS_CC after the last function argument and declare your utility function with TSRMLS_DC after the last declared argument. Always refer to the globals in your function as MYPHPEXT_G(variable). You are encouraged to rename these macros something shorter, see examples in any other php module directory. */ #ifdef ZTS #define MYPHPEXT_G(v) TSRMG(myphpext_globals_id, zend_myphpext_globals *, v) #else #define MYPHPEXT_G(v) (myphpext_globals.v) #endif #endif /* PHP_MYPHPEXT_H */ /* * 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 */ myphpext.c 文件:
+----------------------------------------------------------------------+
| PHP Version 5 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2007 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_01.txt | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | [email protected] so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: | +----------------------------------------------------------------------+ */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "php.h" #include "php_ini.h" #include "ext/standard/info.h" #include "php_myphpext.h" ZEND_DECLARE_MODULE_GLOBALS(myphpext) */ static int le_myphpext; * * Every user visible function must have an entry in myphpext_functions[]. */ zend_function_entry myphpext_functions[] = PHP_FE(confirm_myphpext_compiled, NULL) PHP_FE(HelloPHP, NULL) }; */ zend_module_entry myphpext_module_entry = #if ZEND_MODULE_API_NO >= 20010901 STANDARD_MODULE_HEADER, #endif "myphpext", myphpext_functions, PHP_MINIT(myphpext), PHP_MSHUTDOWN(myphpext), PHP_RINIT(myphpext), PHP_RSHUTDOWN(myphpext), PHP_MINFO(myphpext), #if ZEND_MODULE_API_NO >= 20010901 "0.1", #endif STANDARD_MODULE_PROPERTIES }; #ifdef COMPILE_DL_MYPHPEXT ZEND_GET_MODULE(myphpext) #endif */ PHP_INI_BEGIN() STD_PHP_INI_ENTRY("myphpext.global_value", "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_myphpext_globals, myphpext_globals) STD_PHP_INI_ENTRY("myphpext.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_myphpext_globals, myphpext_globals) PHP_INI_END() */ */ static void php_myphpext_init_globals(zend_myphpext_globals *myphpext_globals) { myphpext_globals->global_value = 0; myphpext_globals->global_string = NULL; } */ */ PHP_MINIT_FUNCTION(myphpext) REGISTER_INI_ENTRIES(); */ return SUCCESS; } */ PHP_MSHUTDOWN_FUNCTION(myphpext) UNREGISTER_INI_ENTRIES(); */ return SUCCESS; } /* Remove if there's nothing to do at request start */ /* {{{ PHP_RINIT_FUNCTION */ PHP_RINIT_FUNCTION(myphpext) { return SUCCESS; } /* }}} */ /* Remove if there's nothing to do at request end */ /* {{{ PHP_RSHUTDOWN_FUNCTION */ PHP_RSHUTDOWN_FUNCTION(myphpext) { return SUCCESS; } /* }}} */ /* {{{ PHP_MINFO_FUNCTION */ PHP_MINFO_FUNCTION(myphpext) { php_info_print_table_start(); php_info_print_table_header(2, "myphpext support", "enabled"); php_info_print_table_end(); /* Remove comments if you have entries in php.ini DISPLAY_INI_ENTRIES(); */ } /* }}} */ /* Remove the following function when you have succesfully 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_myphpext_compiled(string arg) Return a string to confirm that the module is compiled in */ PHP_FUNCTION(confirm_myphpext_compiled) { char *arg = NULL; int arg_len, len; char *strg; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) { return; } len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "myphpext", arg); RETURN_STRINGL(strg, len, 0); } PHP_FUNCTION(HelloPHP) { php_printf("Hello, PHP v5.2.5 - 2008-3-28"); } /* }}} */ /* 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. */ /* * 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 */ 注意本例定义了一个函数 HelloPHP。在 php_myphpext.h 文件中定义: 在 myphpext.c 中有 2 处地方: PHP_FE(HelloPHP, NULL) 语句把我们自己的函数加入入口数组中。 以下定义了 HelloPHP 函数的内容: 其实还有个 confirm_myphpext_compiled 函数,是自动产生的,用于测试,与我们的自定义函数用法一模一样。 5,编译、链接,生成最终的文件。 C:\php-5.2.5-Win32\ext>msdev myphpext\myphpext.dsp /MAKE "myphpext - Win32 Release_TS" php_myphpext.dll - 0 error(s), 0 warning(s) 最终在 C:\php-5.2.5-Win32\Release_TS 下生成了扩展库 php_myphpext.dll。 6,部署: 把 php_myphpext.dll 拷贝到 C:\php-5.2.5-Win32\ext 文件夹下。修改 php.ini 文件: 加语句 extension=php_myphpext.dll。 再注意 extension 路径的指向,需要把 ;extension_dir = "./" 语句的注释去掉,再修改为 extension_dir = "C:\php-5.2.5-Win32\ext"。 最后一定要重启 IIS 服务器。 7,测试: 把 myphpext.php 拷贝到 Web 服务器根下(myphpext.php 的代码也值得一看),在本机用浏览器打开:http://localhost/myphpext.php,应该能看到以下信息: Functions available in the test extension: Congratulations! You have successfully modified ext/myphpext/config.m4. Module myphpext is now compiled into PHP. 再建立一个 test.php 文件,内容为:
<?php
HelloPHP(); ?>
用本机浏览器打开:http://localhost/test.php,应该能看到以下信息: Hello, PHP v5.2.5 - 2008-3-28 这说明我们所有的步骤都是正确的,已经生成了一个自己的 PHP 扩展函数库。只要对 C
语言熟悉,就可以编写大量的自定义函数,供所有人调用。注意,不像 PEAR 等函数库,需要首先在 PHP
代码里指定其文件名才能使用其中的函数和类。PHP 扩展的函数不是用 PHP 语言自身开发的,而是 C 开发的,而且可以直接在 PHP
代码里调用。这样既有一定的保密性,还有效率上的优势。 |
2022-08-17
2022-11-06
2022-08-17
2022-07-29
2022-07-30
请发表评论