学习PHP C扩展之面向对象开发方式 (转)
PHP OOP面向对象之C语言开发方式
学习PHP C扩展有一段时间了,PHP手册里大部分讲的PHP的函数开发方式,网上找OOP资料比较少,想起上个月测试redis 的时候,下载PHP扩展redis源代码是OOP方式的。所以拿来练练手,其它不说了开始
下面要扩展的代码是 ModelHelper.php
< ?php
/**
* 助手类,用于放置需要使用的公共函数和常量
*
*/
class ModelHelper {
static public function escapePage($page)
{
return max(1, intval($page));
}
/*
* 返回基于当前时间的唯一MD5哈希值
*/
static public function uuid()
{
return md5(rand());
}
}
?>
然入 php源代码 ext\ext_skel –extname=ModelHelper 下面是进行修改的c代码
ModelHelper.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 226204 2007-01-01 19:32:10Z iliaa $ */ #ifdef HAVE_CONFIG_H
#include "config.h"
#endif #include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_ModelHelper.h"
#include "ext/standard/php_standard.h"
#include <zend_exceptions .h>
#define MAX(a, b) (((a)>(b))?(a):(b)) /* If you declare any globals in php_ModelHelper.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(ModelHelper)
*/ /* True global resources - no need for thread safety here */
static int le_ModelHelper; static zend_class_entry *ModelHelper_ce;
static zend_class_entry *ModelHelper_exception_ce;
static zend_class_entry *spl_ce_RuntimeException = NULL; /* {{{ ModelHelper_functions[]
*
* Every user visible function must have an entry in ModelHelper_functions[].
*/
static zend_function_entry ModelHelper_functions[] = {
PHP_ME(ModelHelper,escapePage, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_ALLOW_STATIC)
PHP_ME(ModelHelper,uuid, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_ALLOW_STATIC)
/* For testing, remove later. */
{NULL, NULL, NULL} /* Must be the last line in ModelHelper_functions[] */
};
/* }}} */ /* {{{ ModelHelper_module_entry
*/
zend_module_entry ModelHelper_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"ModelHelper",
ModelHelper_functions,
PHP_MINIT(ModelHelper),
PHP_MSHUTDOWN(ModelHelper),
PHP_RINIT(ModelHelper), /* Replace with NULL if there's nothing to do at request start */
PHP_RSHUTDOWN(ModelHelper), /* Replace with NULL if there's nothing to do at request end */
PHP_MINFO(ModelHelper),
#if ZEND_MODULE_API_NO >= 20010901
"0.1", /* Replace with version number for your extension */
#endif
STANDARD_MODULE_PROPERTIES
};
/* }}} */ #ifdef COMPILE_DL_MODELHELPER
ZEND_GET_MODULE(ModelHelper)
#endif /* {{{ PHP_INI
*/
/* Remove comments and fill if you need to have entries in php.ini
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("ModelHelper.global_value", "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_ModelHelper_globals, ModelHelper_globals)
STD_PHP_INI_ENTRY("ModelHelper.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_ModelHelper_globals, ModelHelper_globals)
PHP_INI_END()
*/
/* }}} */ /* {{{ php_ModelHelper_init_globals
*/
/* Uncomment this function if you have INI entries
static void php_ModelHelper_init_globals(zend_ModelHelper_globals *ModelHelper_globals)
{
ModelHelper_globals->global_value = 0;
ModelHelper_globals->global_string = NULL;
}
*/
/* }}} */ /* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(ModelHelper)
{
/* If you have INI entries, uncomment these lines
REGISTER_INI_ENTRIES();
*/ zend_class_entry ModelHelper_class_entry;
INIT_CLASS_ENTRY(ModelHelper_class_entry, "ModelHelper", ModelHelper_functions);
ModelHelper_ce = zend_register_internal_class(&ModelHelper_class_entry TSRMLS_CC);
return SUCCESS;
}
/* }}} */ /* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(ModelHelper)
{
/* 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(ModelHelper)
{
return SUCCESS;
}
/* }}} */ /* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(ModelHelper)
{
return SUCCESS;
}
/* }}} */ /* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(ModelHelper)
{
php_info_print_table_start();
php_info_print_table_header(2, "ModelHelper support", "enabled");
php_info_print_table_row(2, "URL", "http://www.hucde.com");
php_info_print_table_row(2, "author", "Summer.Hu");
php_info_print_table_end(); /* Remove comments if you have entries in php.ini
DISPLAY_INI_ENTRIES();
*/
}
/* }}} */ PHP_METHOD(ModelHelper,escapePage)
{
long return_i;
zval **n;
if (zend_get_parameters_ex(1, &n) == FAILURE) {
WRONG_PARAM_COUNT;
} convert_to_long_ex(n);
return_i=MAX(1,Z_LVAL_PP(n));
RETURN_LONG(return_i);
//convert_to_long_base(return_value, 10);
} PHP_METHOD(ModelHelper,uuid)
{
long number;
number = php_rand(TSRMLS_C);
PHP_MD5_CTX context;
unsigned char digest[16];
char md5str[33];
char string_number[25];
int len;
char *strg;
len = spprintf(&strg, 0, "%u",number);
PHP_MD5Init(&context);
PHP_MD5Update(&context,strg,len);
PHP_MD5Final(digest, &context);
make_digest(md5str, digest);
RETVAL_STRING(md5str, 1);
}
</zend_exceptions>
php_ModelHelper.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 226204 2007-01-01 19:32:10Z iliaa $ */ #ifndef PHP_MODELHELPER_H
#define PHP_MODELHELPER_H extern zend_module_entry ModelHelper_module_entry;
#define phpext_ModelHelper_ptr &ModelHelper_module_entry #ifdef PHP_WIN32
#define PHP_MODELHELPER_API __declspec(dllexport)
#else
#define PHP_MODELHELPER_API
#endif #ifdef ZTS
#include "TSRM.h"
#endif PHP_MINIT_FUNCTION(ModelHelper);
PHP_MSHUTDOWN_FUNCTION(ModelHelper);
PHP_RINIT_FUNCTION(ModelHelper);
PHP_RSHUTDOWN_FUNCTION(ModelHelper);
PHP_MINFO_FUNCTION(ModelHelper); PHP_METHOD(ModelHelper,escapePage);
PHP_METHOD(ModelHelper,uuid);
/* For testing, remove later. */ /*
Declare any global variables you may need between the BEGIN
and END macros here: ZEND_BEGIN_MODULE_GLOBALS(ModelHelper)
long global_value;
char *global_string;
ZEND_END_MODULE_GLOBALS(ModelHelper)
*/ /* In every utility function you add that needs to use variables
in php_ModelHelper_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 MODELHELPER_G(variable). You are
encouraged to rename these macros something shorter, see
examples in any other php module directory.
*/ #ifdef ZTS
#define MODELHELPER_G(v) TSRMG(ModelHelper_globals_id, zend_ModelHelper_globals *, v)
#else
#define MODELHELPER_G(v) (ModelHelper_globals.v)
#endif #endif /* PHP_MODELHELPER_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
*/
然后编译,生成后输入 phpinfo.php 找到ModelHelper
编译成功,然后输入测试一下:
< ?
//$obj=new ModelHelper();
//echo $obj->escapePage(5);
//echo $obj->escapePage(-1);
//echo echo qsyhcd_md5();
//echo $obj->uuid();
//var_dump($obj); echo ModelHelper::escapePage(5)."<br />";
echo ModelHelper::escapePage(-1)."<br />";
echo ModelHelper::escapePage("d'")."<br />";
echo ModelHelper::uuid()."<br />";
?>
转载from:http://www.hucde.com/2010/03/18/29
学习PHP C扩展之面向对象开发方式 (转)的更多相关文章
- MVC学习系列——ModelBinder扩展
在MVC系统中,我们接受数据,运用的是ModelBinder 的技术. MVC学习系列——ActionResult扩展在这个系列中,我们自定义了XmlResult的返回结果. 那么是不是意味着能POS ...
- [java学习笔记]java语言核心----面向对象之this关键字
一.this关键字 体现:当成员变量和函数的局部变量重名时,可以使用this关键字来区别:在构造函数中调用其它构造函数 原理: 代表的是当前对象. this就是所在函数 ...
- Python学习系列(八)( 面向对象基础)
Python学习系列(八)( 面向对象基础) Python学习系列(七)( 数据库编程) 一,面向对象 1,域:属于一个对象或类的变量.有两种类型,即实例变量—属于每个实例/类的对象:类变量—属于类 ...
- python学习-第四天补充-面向对象
python学习-第四天补充-面向对象 python 私有 --name mangling(名字修改.名字) 在命名时,通过使用两个下划线作为开头,可以使得这个变量或者函数编程私有的,但是这个其实的p ...
- JavaSE学习笔记(3)---面向对象三大特性
JavaSE学习笔记(3)---面向对象三大特性 面向对象的三大特征:继承.封装.多态 1.封装 面向对象编程语言是对客观世界的模拟,客观世界里成员变量都是隐藏在对象内部的,外界无法直接操作和修改.然 ...
- JavaSE学习笔记(2)---面向对象基础
JavaSE学习笔记(2)---面向对象基础 1.面向对象具有三大特征:封装性.继承性和多态性,而面向过程没有继承性和多态性,并且面向过程的封装只是封装功能,而面向对象可以封装数据和功能.所以面向对象 ...
- 编程学习笔记(第三篇)面向对象技术高级课程:绪论-软件开发方法的演化与最新趋势(3)软件开发的现状、UML扩展
一.软件开发的现状 软件领域正在发生一个巨变,特别是近几年来,软件领域正在发生翻天覆地的变化. 这一变化主要以这个云 + 端大数据, 这些是随着目前最先进的一些技术的产生而产生的. 随着这些新的技术以 ...
- 面向对象开发方式的开源硬件--.NET Gadgeteer
说起.NET Gadgeteer,不得不先说一下.NET Micro Framework,虽然.NET Micro Framework已经有十几年的发展历史了,但是在全球范围内,.NET Micro ...
- C#学习笔记(六)——面向对象编程简介
一.面向对象编程的含义 * 是一种模块化编程方法,使代码的重用性大大的增加. * oop技术使得项目的设计阶段需要的精力大大的增加,但是一旦对某种类型的数据表达方式达成一致,这种表达方式就可以 ...
随机推荐
- 【Ural】【1057】Amount of degrees
数位DP 2009年刘聪<浅谈数位类统计问题> 例题一 从组合数 以及 数位DP的角度都可以做…… 首先转化成求1~n内K进制下只有0.1的数的个数: 考虑K进制下第一个为1的位,剩下的数 ...
- Java学习第五篇:二进制(原码 反码 补码),位运算,移位运算,约瑟夫问题
一.二进制,位运算,移位运算 1.二进制 对于原码, 反码, 补码而言, 需要注意以下几点: (1).Java中没有无符号数, 换言之, Java中的数都是有符号的; (2).二进制的最高位是符号位, ...
- java后台生成zip打包文件
/** * * @param zipFile 压缩包文件对象 * @param listKey 压缩的图片物理地址 * @return */ public static boolean package ...
- 【面试题032】从1到n整数中1出现的次数
[面试题032]从1到n整数中1出现的次数 题目: 输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数. 例如输入12,从1到12这些整数中包含1的数字有1,10,11和1 ...
- POJ 3277 City Horizon(叶子节点为[a,a+1)的线段树+离散化)
网上还有用unique函数和lowerbound函数离散的方法,可以百度搜下题解就有. 这里给出介绍unique函数的链接:http://www.cnblogs.com/zhangshu/archiv ...
- django如何用orm增加manytomany关系字段(自定义表名)
不自定义表名的,网上有现成的,但如果自定义之后,则要变通一下了. app_insert = App.objects.get(name=app_name) site_insert = Site.obje ...
- ssh 公钥
以前做ssh key登录方式时都是用的root帐号,基本都是无往不利的,权限采用默认即可.今天实验中两台机器帐号都是nianzong,一个普通的帐号.按照如下步骤: A机器:ssh-keygen -t ...
- [转]Java数组初始化详解
一维数组1) int[] a; //声明,没有初始化 2) int[] a=new int[5]; //初始化为默认值,int型为0 3) int[] a={1,2,3,4,5}; ...
- java打印时间精确到毫秒
package net.floodlightcontroller.conflict; import java.io.*; import java.util.*; import java.text.Si ...
- lintcode:Valid Sudoku 判断数独是否合法
题目: 判断数独是否合法 请判定一个数独是否有效.该数独可能只填充了部分数字,其中缺少的数字用 . 表示. 样例 下列就是一个合法数独的样例. 注意 一个合法的数独(仅部分填充)并不一定是可解的.我们 ...