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. 【BZOJ】【1385】【Baltic2000】Division expression

    欧几里得算法 普通的求个gcd即可……思路题 因为要求尽量是整数……所以 $\frac{x_1}{x_2*x_3*x_4*....*x_n}$是最大的结果了,因为$x_2$必须为分母,$x_1$必须为 ...

  2. Mac OS X 安装并测试 OpenCV

    1. 安装 打开官网的Linux安装OpenCV的网页,打开这个网页的目的不是按照它所提供的步骤安装OpenCV(因为你会遇到一个坑,下文会提到),而是为了安装一些依赖的包或库. 其中的pkg-con ...

  3. uva 11174

    刘书上例题 #include <cstdio> #include <cstdlib> #include <cmath> #include <map> # ...

  4. 在MySQL中使用init-connect与binlog来实现用户操作追踪记录

    在MySQL中使用init-connect与binlog来实现用户操作追踪记录 分类: MySQL 前言: 测试环境莫名其妙有几条重要数据被删除了,由于在binlog里面只看到是公用账号删除的,无法查 ...

  5. DataGridView之行的展开与收缩

    很多数据都有父节点与子节点,我们希望单击父节点的时候可以展开父节点下的子节点数据. 比如一个医院科室表,有父科室与子科室,点击父科室后,在父科室下面可以展现该科室下的所有子科室. 我们来说一下在Dat ...

  6. TDD三个阶段

    TDD的三个阶段 红灯.绿灯.重构 :明确了实施TDD所要遵循的工作流 (需求--->测试-->代码[重构]) 红灯阶段:      为不存在的代码编写测试 绿灯阶段:      仅编写适 ...

  7. 安装ubuntu vi编辑无法正常使用的时候 如方向键变成ABCD

    http://blog.sina.com.cn/s/blog_7e3f6e8f0100vkon.html 在使用ubuntu的时候,发现vi编辑模式下退格键backspace和上下左右光标移动键不能用 ...

  8. 关于fisher判别的一点理解

    最近一个朋友问这方面的一些问题,其实之前也就很粗略的看了下fisher,真正帮别人解答问题的时候才知道原来自己也有很多东西不懂.下面小结下自己对fisher判别的理解: 其实fisher和PCA差不多 ...

  9. Error building Player: Win32Exception: ApplicationName=‘xxxxxxxxxxxxxxxxxx//sdk\tools\zipalign.exe' , CommandLine='4 的解决办法

    更新了安卓SDK后,有时候Unity编译失失败,报错类似 Error building Player: Win32Exception: ApplicationName='D:/Program File ...

  10. import java.util.Scanner;

    一.扫描控制台输入     当通过new Scanner(System.in)创建一个Scanner,控制台会一直等待输入,,,,,,,直到敲回车键结束,把所输入的内容传给Scanner,作为扫描对象 ...