学习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技术使得项目的设计阶段需要的精力大大的增加,但是一旦对某种类型的数据表达方式达成一致,这种表达方式就可以 ...
随机推荐
- java文件操作(输出目录、查看磁盘符)
问题描述: java操作文件,所有硬盘中所有文件路径 问题解决: (1)查看所有磁盘文件 注: 如上所示,使用接口 File.listRoots()可以返回所有磁盘文件,通过f ...
- unity3d结合轮廓显示,实现完整的框选目标(附Demo代码)
原地址:http://dong2008hong.blog.163.com/blog/static/469688272013111554511948/ 在unity里实现,其实很简单,因为有两个前提:1 ...
- 如何使用yum来下载RPM包而不进行安装
如何使用yum来下载RPM包而不进行安装 2015-03-23 13:15 theo-l译 linux.cn 字号:T | T yum是基于Red Hat的系统(如CentOS.Fedora.RHEl ...
- CSS 的命名和书写
CSS书写顺序 1.位置属性(position, top, right, z-index, display, float等) 2.大小(width, height, padding, margin) ...
- Jedis 操作
http://www.cnblogs.com/liuling/p/2014-4-19-04.html
- A. Sorting Railway Cars
A. Sorting Railway Cars time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- PHP输入流php://input [转]
对于php://input介绍,PHP官方手册文档有一段话对它进行了很明确地概述. "php://input allows you to read raw POST data. It is ...
- 应用程序出现挂死,.NET Runtime at IP 791F7E06 (79140000) with exit code 80131506.
工具出现挂死问题 1.问题描述 工具出现挂死问题,巡检IIS发现以下异常日志 现网系统日志: 事件类型: 错误 事件来源: .NET Runtime 描述: Application: Di ...
- Linux开机启动流程
开机过程指的是从打开计算机电源直到LINUX显示用户登录画面的全过程: 1)加载BIOS 2)读取MBR 3)Boot Loader 4)加载内核 ...
- iOS开发--SWRevealViewController
SWRevealViewController 是一个比较好的实现Sliding功能的第三方类. 今天跟大家分享一下如何使用SWRevealViewController实现左右菜单滑动功能. 首先,在工 ...