php扩展开发---将类赋值给zval变量-object_init_ex

来源:互联网 发布:声音抓取软件 编辑:程序博客网 时间:2024/05/17 22:23

php version:5.6.22
mac


<?php$http = new xing_http_server();$http->on("start",function($server){    echo "http server is started at http://127.0.0.1:9501\n";    echo $server . PHP_EOL ;});$http->on("request", function ($request, $response) {    $response->header("Content-Type", "text/plain");    //$response->end("Hello World\n");    echo $request . PHP_EOL;    //echo $response . PHP_EOL;});//$http->start();
/*  +----------------------------------------------------------------------+  | PHP Version 5                                                        |  +----------------------------------------------------------------------+  | Copyright (c) 1997-2016 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$ */#ifdef HAVE_CONFIG_H#include "config.h"#endif//#include <Zend/zend_interfaces.h>#include "php_xing2233.h"/* If you declare any globals in php_xing2233.h uncomment this:ZEND_DECLARE_MODULE_GLOBALS(xing2233)*//* True global resources - no need for thread safety here */static int le_xing2233;/* {{{ PHP_INI *//* Remove comments and fill if you need to have entries in php.iniPHP_INI_BEGIN()    STD_PHP_INI_ENTRY("xing2233.global_value",      "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_xing2233_globals, xing2233_globals)    STD_PHP_INI_ENTRY("xing2233.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_xing2233_globals, xing2233_globals)PHP_INI_END()*//* }}} *//* Remove the following function when you have successfully modified config.m4   so that your module can be compiled into PHP, it exists only for testing   purposes. *//* Every user-visible function in PHP should document itself in the source *//* {{{ proto string confirm_xing2233_compiled(string arg)   Return a string to confirm that the module is compiled in */PHP_FUNCTION(confirm_xing2233_compiled){    char *arg = NULL;    int arg_len, len;    char *strg;    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {        return;    }    zval *args;    MAKE_STD_ZVAL(args);    array_init(args);    RETURN_ZVAL(args, 0, 1);    len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "xing2233", arg);    RETURN_STRINGL(strg, len, 0);}/* }}} *//* The previous line is meant for vim and emacs, so it can correctly fold and   unfold functions in source code. See the corresponding marks just before   function definition, where the functions purpose is also documented. Please   follow this convention for the convenience of others editing your code.*//* {{{ php_xing2233_init_globals *//* Uncomment this function if you have INI entriesstatic void php_xing2233_init_globals(zend_xing2233_globals *xing2233_globals){    xing2233_globals->global_value = 0;    xing2233_globals->global_string = NULL;}*//* }}} *//* {{{ PHP_MINIT_FUNCTION *///int time_of_minit;//static zend_function_entry myclass_method[] = {//        PHP_FE_END//};//zend_class_entry *myclass_ce;//int moudle_number = 1;//不能出现和其他扩展相同的方法名//void xing_http_server_init(TSRMLS_D)//{//    zend_class_entry ce;//  INIT_CLASS_ENTRY(ce, "Xing\\myclass", myclass_method);//  myclass_ce = zend_register_internal_class(&ce TSRMLS_CC);////    zend_declare_property_string(myclass_ce, "name", sizeof("name")-1, "", ZEND_ACC_PUBLIC TSRMLS_CC);////    return SUCCESS;//}zend_class_entry xing2233_http_server_ce;zend_class_entry *xing2233_http_server_class_entry_ptr;//responsezend_class_entry xing2233_http_server_ce_response;zend_class_entry *xing2233_http_server_class_entry_ptr_response;PHP_METHOD(xing_http_server, on){//    php_printf("this is on");    zval *callback;    zval *event_name;    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &event_name, &callback) == FAILURE)    {        php_printf("failure");    }    if (strncasecmp("request", Z_STRVAL_P(event_name), Z_STRLEN_P(event_name)) == 0)    {        //判段event_name是否等于request        //判断callback是不是回调方法        char *func_name = NULL;        if (!zend_is_callable(callback, 0, &func_name TSRMLS_CC)) {            php_error_docref(NULL TSRMLS_CC, E_ERROR, "function '%s' is not callable", func_name);            efree(func_name);            RETURN_FALSE;        }        efree(func_name);        //回调方法参数赋值        zval *args[2];        MAKE_STD_ZVAL(args[0]);        MAKE_STD_ZVAL(args[1]);        ZVAL_STRING(args[0], "request", 1);//        ZVAL_STRING(args[1], "response", 1);        object_init_ex(args[1], xing2233_http_server_class_entry_ptr_response);        if (call_user_function(EG(function_table), NULL, callback, return_value, 2, args TSRMLS_CC) != SUCCESS) {            php_printf("callback faild");            RETURN_FALSE;        }        zval_ptr_dtor(&args[0]);        zval_ptr_dtor(&args[1]);    }    else    {        //判断callback是不是回调方法        char *func_name = NULL;        if (!zend_is_callable(callback, 0, &func_name TSRMLS_CC)) {            php_error_docref(NULL TSRMLS_CC, E_ERROR, "function '%s' is not callable", func_name);            efree(func_name);            RETURN_FALSE;        }        efree(func_name);        //回调方法参数赋值        zval *args[1];        MAKE_STD_ZVAL(args[0]);        ZVAL_LONG(args[0], 1);        if (call_user_function(EG(function_table), NULL, callback, return_value, 1, args TSRMLS_CC) != SUCCESS) {            php_printf("callback faild");            RETURN_FALSE;        }        zval_ptr_dtor(&args[0]);    }//    efree(func_name);}PHP_METHOD(xing_http_server_reponse, header){    char *header_one;    char *header_two;    int header_one_len, header_two_len;    if (zend_parse_parameters(            ZEND_NUM_ARGS() TSRMLS_CC,            "ss",            &header_one,            &header_one_len,            &header_two,            &header_two_len) == FAILURE)    {        php_printf("failure");    }    PHPWRITE(header_one, header_one_len);}static zend_function_entry xing2233_http_server_methods[] = {        PHP_ME(xing_http_server, on, NULL, ZEND_ACC_PUBLIC)        PHP_FE_END};static zend_function_entry xing2233_http_server_methods_response[] = {        PHP_ME(xing_http_server_reponse, header, NULL, ZEND_ACC_PUBLIC)        PHP_FE_END};PHP_MINIT_FUNCTION(xing2233){    /* If you have INI entries, uncomment these lines    REGISTER_INI_ENTRIES();    */    INIT_CLASS_ENTRY(xing2233_http_server_ce, "xing_http_server", xing2233_http_server_methods);    xing2233_http_server_class_entry_ptr = zend_register_internal_class(&xing2233_http_server_ce TSRMLS_CC);    INIT_CLASS_ENTRY(xing2233_http_server_ce_response, "xing_http_server_response", xing2233_http_server_methods_response);    xing2233_http_server_class_entry_ptr_response = zend_register_internal_class(&xing2233_http_server_ce_response TSRMLS_CC);    return SUCCESS;}/* }}} *//* {{{ PHP_MSHUTDOWN_FUNCTION */PHP_MSHUTDOWN_FUNCTION(xing2233){    /* uncomment this line if you have INI entries    UNREGISTER_INI_ENTRIES();    *///    FILE *fp=fopen("/Users/albert/Documents/php/php-5.6.22/ext/xing2233/m.txt","a+");//    fprintf(fp, "%d\n", time_of_minit);//    fclose(fp);//  free(module_number);    return SUCCESS;}/* }}} *//* Remove if there's nothing to do at request start *//* {{{ PHP_RINIT_FUNCTION *///int time_of_rinit;PHP_RINIT_FUNCTION(xing2233){//    time_of_rinit = time(NULL);    return SUCCESS;}/* }}} *//* Remove if there's nothing to do at request end *//* {{{ PHP_RSHUTDOWN_FUNCTION */PHP_RSHUTDOWN_FUNCTION(xing2233){//    FILE *fp=fopen("/Users/albert/Documents/php/php-5.6.22/ext/xing2233/r.txt","a+");//    fprintf(fp, "%d\n", time_of_rinit);//    fclose(fp);    return SUCCESS;}/* }}} *//* {{{ PHP_MINFO_FUNCTION */PHP_MINFO_FUNCTION(xing2233){    php_info_print_table_start();    php_info_print_table_header(2, "xing2233 support", "enabled");    php_info_print_table_end();    /* Remove comments if you have entries in php.ini    DISPLAY_INI_ENTRIES();    */}/* }}} *//* {{{ xing2233_functions[] * * Every user visible function must have an entry in xing2233_functions[]. */const zend_function_entry xing2233_functions[] = {    PHP_FE(confirm_xing2233_compiled,   NULL)       /* For testing, remove later. */    PHP_FE_END  /* Must be the last line in xing2233_functions[] */};/* }}} *//* {{{ xing2233_module_entry */zend_module_entry xing2233_module_entry = {    STANDARD_MODULE_HEADER,    "xing2233",    xing2233_functions,    PHP_MINIT(xing2233),    PHP_MSHUTDOWN(xing2233),    PHP_RINIT(xing2233),        /* Replace with NULL if there's nothing to do at request start */    PHP_RSHUTDOWN(xing2233),    /* Replace with NULL if there's nothing to do at request end */    PHP_MINFO(xing2233),    PHP_XING2233_VERSION,    STANDARD_MODULE_PROPERTIES};/* }}} */#ifdef COMPILE_DL_XING2233ZEND_GET_MODULE(xing2233)#endif/* * 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 */
原创粉丝点击