用PHP扩展做一个HelloWorld!

小辰酱_3032

小辰酱_3032

2016-07-11

1700人浏览

原创

PHP 尽管提供了大量有用的函数,但是在特殊情况下还可能需要进行扩展编程,比如大量的 PECL(PHP Extension Community Library)就是以扩展的形式提供的(动态链接库dll文件),它们比 PEAR 的运行效率要高很多。
    PHP 扩展是用 C 或 C++ 编写的,需要编译成动态连接库 dll 文件后在 PHP 环境下注册后才能使用。
    编写 PHP 扩展的软件要求:
      VC++6.0 或 VC++.NET 环境。
      PHP 的源代码,需要编译。
    如果不愿意编译 PHP 的源代码,可以再下载 PHP 的已经编译成功的二进制代码(就是我们部署 PHP 运行环境的那些文件包)。注意分别下载的源文件包和已编译包,它们的版本必须一致。

    过程:

    1,安装 VC++6.0,并选择把其可执行文件路径加入环境变量中,使在命令行环境任意路径下可以运行编译器。
    2,安装 PHP 运行环境,并与 IIS 正确集成在一起。假设使用的 PHP 版本为 5.2.5,下载 php-5.2.5-Win32.zip 二进制包和 php-5.2.5.tar.gz 源代码包。安装环境为 C:\php-5.2.5-Win32。分别把源代码包和二进制包解压到该文件夹下。从 php.ini-recommended 拷贝生成一个 php.ini 文件。
    3,建立 C:\php-5.2.5-Win32\Release_TS 文件夹,拷贝 C:\php-5.2.5-Win32\dev\php5ts.lib 文件到这里。
    4,进入 C:\php-5.2.5-Win32\ext 文件夹,运行命令:
      C:\php-5.2.5-Win32\ext>..\php.exe ext_skel_win32.php --extname=myphpext
      Creating directory myphpext
      Creating basic files: config.m4 config.w32 .cvsignore myphpext.c php_myphpext.h
      CREDITS EXPERIMENTAL tests/001.phpt myphpext.php [done].

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

      1.  $ cd ..
      2.  $ vi ext/myphpext/config.m4
      3.  $ ./buildconf
      4.  $ ./configure --[with|enable]-myphpext
      5.  $ make
      6.  $ ./php -f ext/myphpext/myphpext.php
      7.  $ vi ext/myphpext/myphpext.c
      8.  $ make

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

    结果在 ext 下生成一个文件夹 myphpext,包含一个 PHP 扩展应用编程框架。myphpext 可以任意取名,将来生成的 dll 文件格式为 php_[extname].dll,我们生成的就是 php_myphpext.dll。运行结果的提示信息 1.2...8 主要是对 Linux/Unix 环境而言的,我们不必理会。其实 config.m4 文件在 Windows 下也可能需要修改,但是对于我们简单的框架暂时还用不着。

    文件夹 myphpext 包含若干个文件,其中:

    myphpext.dsp 是工程文件,后边还要用;
    myphpext.php 扩展测试文件;
    php_myphpext.h 扩展函数定义头文件
    myphpext.c 扩展函数具体实现

    以上 2 个重要的文件内容:

HTML5技术开发服务解决公司网站模板
HTML5技术开发服务解决公司网站模板

HTML5技术开发服务解决公司网站模板是一款提供为企业提供服务和解决方案的公司宣传网站模板下载。提示:本模板调用到谷歌字体库,可能会出现页面打开比较缓慢。

下载

    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          |
      | license@php.net 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      
*/


  

  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          |
      | license@php.net 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 $ */

    #ifdef HAVE_CONFIG_H
    #include 
"config.h"
    
#endif

    #include 
"php.h"
    #include 
"php_ini.h"
    #include 
"ext/standard/info.h"
    #include 
"php_myphpext.h"

    
/* If you declare any globals in php_myphpext.h uncomment this:
    ZEND_DECLARE_MODULE_GLOBALS(myphpext)
    
*/


    
/* True global resources - no need for thread safety here */
    
static int le_myphpext;

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

    zend_function_entry myphpext_functions[] 
= {
     PHP_FE(confirm_myphpext_compiled, NULL)  
/* For testing, remove later. */
     PHP_FE(HelloPHP, NULL)
     
{NULL, NULL, NULL} /* Must be the last line in myphpext_functions[] */
    }
;
    
/* }}} */

    
/* {{{ myphpext_module_entry
     
*/

    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),  
/* Replace with NULL if there's nothing to do at request start */
     PHP_RSHUTDOWN(myphpext), 
/* Replace with NULL if there's nothing to do at request end */
     PHP_MINFO(myphpext),
    
#if ZEND_MODULE_API_NO >= 20010901
     
"0.1", /* Replace with version number for your extension */
    
#endif
     STANDARD_MODULE_PROPERTIES
    }
;
    
/* }}} */

    #ifdef COMPILE_DL_MYPHPEXT
    ZEND_GET_MODULE(myphpext)
    
#endif

    
/* {{{ PHP_INI
     
*/

    
/* Remove comments and fill if you need to have entries in php.ini
    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()
    
*/

    
/* }}} */

    
/* {{{ php_myphpext_init_globals
     
*/

    
/* Uncomment this function if you have INI entries
    static void php_myphpext_init_globals(zend_myphpext_globals *myphpext_globals)
    {
     myphpext_globals->global_value = 0;
     myphpext_globals->global_string = NULL;
    }
    
*/

    
/* }}} */

    
/* {{{ PHP_MINIT_FUNCTION
     
*/

    PHP_MINIT_FUNCTION(myphpext)
    
{
     
/* If you have INI entries, uncomment these lines
     REGISTER_INI_ENTRIES();
     
*/

     
return SUCCESS;
    }

    
/* }}} */

    
/* {{{ PHP_MSHUTDOWN_FUNCTION
     
*/

    PHP_MSHUTDOWN_FUNCTION(myphpext)
    
{
     

php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!

相关文章

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

php扩展 编程语言

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
GoFrame框架Web开发与路由中间件
GoFrame框架Web开发与路由中间件

本专题聚焦GoFrame框架的Web开发,拆解路由注册与匹配规则、请求参数获取、参数校验与中间件执行顺序,说明统一响应、JWT鉴权、文件上传与接口文档生成的落地方式,并补充优雅重启与测试实践。

2026.11.17

0

15

GoFrame框架安装与gf命令行工具
GoFrame框架安装与gf命令行工具

本专题围绕GoFrame框架的安装与工具链配置展开,讲清Go版本要求、gf命令行的安装方式、代理镜像与依赖准备,说明项目初始化、热编译、交叉编译打包的做法,并整理安装阶段的常见报错与验证步骤。

2026.11.17

0

15

GoFrame框架零基础入门
GoFrame框架零基础入门

本专题整理GoFrame框架的入门知识,从模块化设计理念、环境准备到跑通第一个Web服务,讲清目录分层、配置加载、日志与中间件的用法,并对比Gin、Go-Zero的差异,帮助Go开发者建立整体认知。

2026.11.17

0

15

SQLAlchemy 一对多关系怎么建立
SQLAlchemy 一对多关系怎么建立

SQLAlchemy 一对多关系怎么建立?本专题整理父子表外键、relationship 双向映射、关联查询、级联删除和加载优化等相关文章,帮助你理解并实现常见的 ORM 关系建模。

2026.11.17

0

10

SQLAlchemy 如何增删改查
SQLAlchemy 如何增删改查

SQLAlchemy 如何增删改查?本专题整理 ORM 新增记录、条件查询、更新和删除、Session 事务提交与批量处理等实用文章,帮助你掌握 Python 数据库操作流程。

2026.11.17

0

11

SQLAlchemy ORM 模型怎么写
SQLAlchemy ORM 模型怎么写

SQLAlchemy ORM 模型怎么写?本专题整理模型类定义、Mapped 字段映射、主键与约束、表结构创建和常见报错的相关文章,帮助你从基础配置到实际建模逐步掌握 SQLAlchemy。

2026.11.17

0

16

Go-Zero API开发使用教程
Go-Zero API开发使用教程

本专题聚焦Go-Zero的API开发,拆解.api描述文件的语法结构、路由与分组定义、参数绑定与校验规则,说明统一响应体、JWT鉴权、跨域与接口文档生成的落地方式,并给出网关聚合与接口测试的实践建议。

2026.11.12

160

15

Go-Zero安装与goctl环境配置教程
Go-Zero安装与goctl环境配置教程

本专题围绕Go-Zero的安装与工具链配置展开,讲清Go版本要求、goctl安装方式、代理镜像、protoc依赖与环境变量设置,说明多版本共存、升级回退与CI流水线安装的做法,并整理安装阶段的常见报错与验证步骤。

2026.11.12

120

15

Go-Zero零基础入门教程
Go-Zero零基础入门教程

本专题整理Go-Zero的入门知识,从框架定位、环境准备到跑通第一个API服务,讲清目录分层、配置加载、日志与中间件的基本用法,并对比Gin、Kratos的差异,帮助Go开发者建立整体认知,判断它是否适合当前项目。

2026.11.12

100

15

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Go编程名库XORM使用教程
Go编程名库XORM使用教程

共0课时 | 0人学习

Gradle 官方入门教程
Gradle 官方入门教程

共0课时 | 0人学习