学习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          |

  | [email protected] 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          |

  | [email protected] 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

你可能感兴趣的:(面向对象)