PHP扩展开发(4)

由于函数和单类的扩展,网上一搜一大片,这里就不再叙述了。

这里特别感谢laruence(鸟哥)开源的yaf扩展,解决困扰我多时的多类问题,还在看他的代码学习中,这里是对多类写法学习的一个阶段总结。

 
 
1. 修改php_simple.h
 
 增加:
#define SIMPLE_STARTUP (module)                    ZEND_MODULE_STARTUP_N(simple_##module)(INIT_FUNC_ARGS_PASSTHRU )
#define SIMPLE_MINIT_FUNCTION (module)                ZEND_MINIT_FUNCTION(simple_##module)
 
2. 添加simple_util.h
 
#ifndef _SIMPLE_UTIL_H_
#define _SIMPLE_UTIL_H_
 
extern zend_class_entry *simple_util_ce;
 
SIMPLE_MINIT_FUNCTION(util);
 
#endif
 
3. 添加simple_util.c
 
#ifdef HAVE_CONFIG_H
#include "config.php"
#endif
 
#include "php.h"
#include "php_ini.h"
#include "Zend/zend_interfaces.h"
 
#include "php_simple.h"
#include "simple_util.h"
 
//这里是定义静态类
static zend_class_entry *simple_util_ce;
 
/** {{{ ARG_INFO
*/
ZEND_BEGIN_ARG_INFO_EX(simple_util_void_arginfo, 0, 0, 0)
ZEND_END_ARG_INFO()
/* }}} */
 
PHP_METHOD(simple_util, read) {
        RETURN_STRING("这里返回函数结果" , 1);
}
 
 
zend_function_entry simple_util_methods[] = {
        PHP_ME(simple_util, read, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC )
       {
               NULL, NULL, NULL
       }
};
 
//相当于把多个扩展合在一起,每个类各自ZEND_MINIT_FUNCTION
SIMPLE_MINIT_FUNCTION(util) {
        zend_class_entry ce;
        INIT_CLASS_ENTRY(ce, "Util" , simple_util_methods);
 
       simple_util_ce = zend_register_internal_class_ex(&ce, NULL, NULL TSRMLS_CC);
 
        return SUCCESS;
}
 
4. 修改simple.c
 
在ZEND_MINIT_FUNCTION里加上
 
SIMPLE_STARTUP(util);
 
结果是:
 
 
PHP_MINIT_FUNCTION(boxun)
{
        REGISTER_INI_ENTRIES();
 
        SIMPLE_STARTUP(util);
 
        return SUCCESS;
}
 
即在模块加载时引入,各类独立,只是写在同一个扩展里,这样写很清晰。 
原文地址:https://www.cnblogs.com/imarno/p/5156670.html