第一步:
下载php源码
进入ext目录,
调用ext_skel.php 生成架构
php ext_skel.php --ext <name>
然后可以把生成的文件中的例子删除掉
在头信息中加入
PHP_METHOD(Children, love);
具体实现看代码
/*
+----------------------------------------------------------------------+
| Copyright (c) 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: |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "php.h"
#include "ext/standard/info.h"
#include "php_mytest.h"
// 声明一个类的结构体
zend_class_entry *children_ce;
// 类的方法 第一次参数是类名,第二个方法名
PHP_METHOD(Children, learn)
{
char *arg = NULL;
int *arg_len;
// 获取字符串的参数,一个s表示一个字符串参数
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
WRONG_PARAM_COUNT;
}
// 更新对象的memory 属性
zend_update_property_string(children_ce,getThis(),"memory",sizeof("memory")-1, arg);
}
// 定义方法的参数
ZEND_BEGIN_ARG_INFO_EX(arginfo_children_learn, 0, 0, 1)
ZEND_ARG_INFO(0, love)
ZEND_END_ARG_INFO()
// 方法数组
const zend_function_entry children_functions[] = {
PHP_ME(Children, learn, arginfo_children_learn, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
// php模块初始化方法
PHP_MINIT_FUNCTION(mytest)
{
zend_class_entry ce;
// 注册一个类
INIT_CLASS_ENTRY(ce, "Children", children_functions);
children_ce = zend_register_internal_class(&ce);
// 初始化属性
zend_declare_property_null(children_ce, ZEND_STRL("memory"), ZEND_ACC_PRIVATE TSRMLS_CC);
return SUCCESS;
}
/* 调用初始化*/
PHP_RINIT_FUNCTION(mytest)
{
#if defined(ZTS) && defined(COMPILE_DL_MYTEST)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
return SUCCESS;
}
/* phpinfo信息 */
PHP_MINFO_FUNCTION(mytest)
{
php_info_print_table_start();
php_info_print_table_header(2, "mytest support", "enabled");
php_info_print_table_end();
}
/* 定义模块结构,一般不需要动*/
zend_module_entry mytest_module_entry = {
STANDARD_MODULE_HEADER,
"mytest", /* Extension name */
children_functions, /* zend_function_entry */
PHP_MINIT(mytest), /* PHP_MINIT - Module initialization */
NULL, /* PHP_MSHUTDOWN - Module shutdown */
PHP_RINIT(mytest), /* PHP_RINIT - Request initialization */
NULL, /* PHP_RSHUTDOWN - Request shutdown */
PHP_MINFO(mytest), /* PHP_MINFO - Module info */
PHP_MYTEST_VERSION, /* Version */
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_MYTEST
# ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
# endif
ZEND_GET_MODULE(mytest)
#endif
运行 phpize
./configure --with-php-config=php-config路径
make && make install
然后修改php配置文件,加入这个扩展
php -m 检查后,试一下代码
php -r '$children = new Children();$children->learn("aa");var_dump($children);'
如果没有段错误,那么就实现了,这是入门篇,关于php类的扩展开发资料并不多,所有我写了一篇入门的,希望大家能够get新技能