根据用户传入的参数调用用户函数的php扩展
2012-03-01 17:05
369 查看
这是目的是一个示例,php本身就有这样的函数
调用
结果:
感兴趣的+qq群:95303036
PHP_FUNCTION(thread_wrok) { zval *cls=NULL,*zvalue=NULL,*z_method; //--------------------------------------------------------- zval ***params=NULL; int arg_count = ZEND_NUM_ARGS(); if (arg_count <1 ) { WRONG_PARAM_COUNT; } params = (zval ***) safe_emalloc(sizeof(zval **), arg_count, 0); if (zend_get_parameters_array_ex(arg_count, params) == FAILURE) { efree(params); RETURN_FALSE; } z_method=*params[0]; if(arg_count>1&¶ms[1]) { cls=*params[1]; } if ( Z_TYPE_P(z_method) != IS_STRING ) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "First param must be string"); RETURN_FALSE; } //--------------------------------------------------------- /* char *method; int method_len; if ( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "s|o", &method, &method_len, &cls) == FAILURE ) { RETURN_FALSE; } MAKE_STD_ZVAL(z_method); ZVAL_STRING(z_method, method, 1); if (method && zend_hash_find(Z_ARRVAL_P(method_records), method, method_len + 1, (void**)&zvalue) != FAILURE) { RETURN_FALSE; } */ //--------------------------------------------------------- zval *retval_ptr; arg_count = arg_count-2>0?arg_count-2:0; if(Z_TYPE_P(cls) == IS_OBJECT || Z_TYPE_P(cls) == IS_STRING ) { if (call_user_function_ex(EG(function_table), &cls , z_method, &retval_ptr, arg_count, arg_count?params+2:NULL , 0, NULL TSRMLS_CC) == SUCCESS) { if (retval_ptr) { zval_ptr_dtor(&retval_ptr); } } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to call class method: %s()", Z_STRVAL_P(z_method)); RETURN_FALSE; } }else{ if (call_user_function_ex(CG(function_table), NULL, z_method, &retval_ptr, arg_count, arg_count?params+2:NULL , 0, NULL TSRMLS_CC) == SUCCESS ) { if (retval_ptr) { zval_ptr_dtor(&retval_ptr); } } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to call %s()", Z_STRVAL_P(z_method)); RETURN_FALSE; } } //efree(z_method); RETURN_TRUE; }
调用
<?php $br = (php_sapi_name() == "cli")? "":"<br>"; if(!extension_loaded('threadwork')) { dl('threadwork.' . PHP_SHLIB_SUFFIX); } class a{ function e($a1=0,$a2="") { echo "method e,$a1,$a2\n"; } } function e($a1=0,$a2="") { echo "function e,$a1,$a2\n"; } $a=new a(); if(thread_wrok("e",$a)) { echo "a-> OK\n"; } else echo "NO\n"; if(thread_wrok("e","a",date("Y-m-d H:i:s"))) { echo "&a->e OK\n"; } if(thread_wrok("e",&$a,date("Y-m-d H:i:s"))) { echo "&a->e OK\n"; } thread_wrok($a); if(thread_wrok("e",NULL,222,'this is str')) { echo "e OK\n"; } else echo "NO\n"; ?>
结果:
method e,0, a-> OK method e,2012-03-01 17:13:21, &a->e OK method e,2012-03-01 17:13:21, &a->e OK Warning: thread_wrok(): First param must be string in /opt/soft/php-5.2.8/ext/threadwork/threadwork.php on line 34 function e,222,this is str e OK
感兴趣的+qq群:95303036
相关文章推荐
- 根据用户传入的参数调用用户函数的php扩展
- 根据用户传入的参数调用用户函数的php扩展
- liunx python调用c++库(类、函数),传入传出参数
- PHP扩展开发入门3------>带参数的函数
- PHP扩展开发系列教程3-接收用户参数
- PHP调用C++扩展时memset/sizeof函数无效的问题
- 编写一个程序,其main()调用一个用户定义的函数(以光年值为参数,并返回对应天文单位 的值).该程序按下面的格式要求用户输入光年值
- php5.6及其以上版本有了新特性,函数可以传入不定数目的参数
- php学习笔记1--根据条件调用不同的函数
- JS 定义函数的参数的个数,传入参数的个数对调用的影响
- tornado学习笔记(一):如何给ioloop.run_sync()中调用的函数传入参数
- Python中将函数作为另一个函数的参数传入并调用
- php中在一个php文件调用另一个php的函数,保存登录时保存的用户id
- PHP扩展开发系列教程3-接收用户参数
- JAVA 通过 JNA 调用 C程序 dll 函数传入中文参数问题
- php扩展开发:编写一个传递引用参数的函数
- asp.net中调用用户控件中的函数并传递参数
- 函数调用时参数传入的几种情况
- js实现调用方法(函数)时可动态传入不同参数个数、模拟实现方法(函数)重载
- PHP根据传入参数合并多个JS和CSS文件的简单实现