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&lt;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扩展之面向对象开发方式 (转)的更多相关文章

  1. MVC学习系列——ModelBinder扩展

    在MVC系统中,我们接受数据,运用的是ModelBinder 的技术. MVC学习系列——ActionResult扩展在这个系列中,我们自定义了XmlResult的返回结果. 那么是不是意味着能POS ...

  2. [java学习笔记]java语言核心----面向对象之this关键字

    一.this关键字 体现:当成员变量和函数的局部变量重名时,可以使用this关键字来区别:在构造函数中调用其它构造函数 原理:         代表的是当前对象.         this就是所在函数 ...

  3. Python学习系列(八)( 面向对象基础)

     Python学习系列(八)( 面向对象基础) Python学习系列(七)( 数据库编程) 一,面向对象 1,域:属于一个对象或类的变量.有两种类型,即实例变量—属于每个实例/类的对象:类变量—属于类 ...

  4. python学习-第四天补充-面向对象

    python学习-第四天补充-面向对象 python 私有 --name mangling(名字修改.名字) 在命名时,通过使用两个下划线作为开头,可以使得这个变量或者函数编程私有的,但是这个其实的p ...

  5. JavaSE学习笔记(3)---面向对象三大特性

    JavaSE学习笔记(3)---面向对象三大特性 面向对象的三大特征:继承.封装.多态 1.封装 面向对象编程语言是对客观世界的模拟,客观世界里成员变量都是隐藏在对象内部的,外界无法直接操作和修改.然 ...

  6. JavaSE学习笔记(2)---面向对象基础

    JavaSE学习笔记(2)---面向对象基础 1.面向对象具有三大特征:封装性.继承性和多态性,而面向过程没有继承性和多态性,并且面向过程的封装只是封装功能,而面向对象可以封装数据和功能.所以面向对象 ...

  7. 编程学习笔记(第三篇)面向对象技术高级课程:绪论-软件开发方法的演化与最新趋势(3)软件开发的现状、UML扩展

    一.软件开发的现状 软件领域正在发生一个巨变,特别是近几年来,软件领域正在发生翻天覆地的变化. 这一变化主要以这个云 + 端大数据, 这些是随着目前最先进的一些技术的产生而产生的. 随着这些新的技术以 ...

  8. 面向对象开发方式的开源硬件--.NET Gadgeteer

    说起.NET Gadgeteer,不得不先说一下.NET Micro Framework,虽然.NET Micro Framework已经有十几年的发展历史了,但是在全球范围内,.NET Micro ...

  9. C#学习笔记(六)——面向对象编程简介

    一.面向对象编程的含义 *   是一种模块化编程方法,使代码的重用性大大的增加. *   oop技术使得项目的设计阶段需要的精力大大的增加,但是一旦对某种类型的数据表达方式达成一致,这种表达方式就可以 ...

随机推荐

  1. Robot Framework 环境搭建

    一.下载软件 1.安装Python 到官网,下载Python 2.7.9:https://www.python.org/downloads/,最好选择32位版本的(64位系统也支付32位版本),然后安 ...

  2. Hibernate3中将指定的HQL语句转换成SQL语句

    import org.hibernate.engine.SessionFactoryImplementor; import org.hibernate.hql.ast.QueryTranslatorI ...

  3. C# MessageBox 用法大全(转)

    C# MessageBox 用法大全 http://www.cnblogs.com/Tammie/archive/2011/08/05/2128623.html 我们在程序中经常会用到MessageB ...

  4. CF 86D Powerful array

    离线+分块 将n个数分成sqrt(n)块. 对所有询问进行排序,排序标准:       1. Q[i].left /block_size < Q[j].left / block_size (块号 ...

  5. LintCode 字符串比较

    比较两个字符串A和B,确定A中是否包含B中所有的字符.字符串A和B中的字符都是大写字母: 给出 A = "ABCD" B = "AABC", 返回 false ...

  6. lintcode: 爬楼梯

    题目: 爬楼梯 假设你正在爬楼梯,需要n步你才能到达顶部.但每次你只能爬一步或者两步,你能有多少种不同的方法爬到楼顶部? 样例 比如n=3,中不同的方法 返回 3 解题: 动态规划题目,同时还是有顺序 ...

  7. 【memcache缓存专题(3)】PHP-memcache扩展的安装以及使用

    安装PHP-memcache扩展和安装其他PHP扩展的步骤是一样的. 安装 step 1:搜索下载扩展 http://pecl.php.net/package/memcache step 2: gzi ...

  8. java代码实现自动登录功能

    通常我们登录某网站,会有选择保存几天,或者是几个星期不用登录,之后输入该网站地址无需登录直接进入主页面,那么这就叫做自动登录,怎么实现呢,下面我以一个小例子来演示一下 登录页面:login.jsp & ...

  9. Hibernate逍遥游记-第12章 映射值类型集合-005对集合排序Map(<order-by>\<sort>)

    1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...

  10. REACTOS(193)与汇编编译器(69)的高人

    REACTOS(193)与汇编编译器(69)的高人http://blog.csdn.net/caimouse ReactOS编译成VS工程1: 首先从https://www.reactos.org/w ...