问题描述
所以我决定写一个PHP扩展。一切似乎除了我被困在一个很小的问题,要罚款。
So I decided to write an extension for php. Everything seems to be fine except I'm stuck on a tiny problem.
我的php-5.4.9
源$ C $ CS。有文件转/标准/ mail.c
与真棒功能
I have php-5.4.9
source codes. There is file ext/standard/mail.c
with awesome function
PHPAPI int php_mail(char *to, char *subject, char *message, char *headers, char *extra_cmd TSRMLS_DC)
在我的延长, acme.c
我有包括
...
#include "php.h"
#include "ext/standard/php_mail.h"
#include "php_ini.h"
...
所以 php_mail
感觉很好,工作正常。但是,很明显,我想用从 mail.c
的code开始在线101和189上结束(的 5-93对应的行)。所以,我回过神来的想法(这是在某些点别扭虽然)为什么不叫 PHP_FUNCTION(邮件)
?到了那一刻我无法找到的宏,实际上我想知道实现这个想法的最好办法。
So php_mail
feels good and works fine. But, obviously, I want to use the code from mail.c
starting on line 101 and ending on 189 (http://pastie.org/5444192 5-93 corresponding lines in the paste). So I caught myself on idea (it is awkward in some point though) why not to call PHP_FUNCTION(mail)
? By the moment I could not locate that macros, and actually I'd like to know the best way to implement the idea.
我内心的Zend工程师(这是新手)建议我使用call_user_function
My inner zend engineer (which is newbie) recommends me to use call_user_function
ZEND_API int call_user_function(HashTable *function_table, zval **object_pp, zval *function_name, zval *retval_ptr, zend_uint param_count, zval *params[] TSRMLS_DC);
但我无法弄清楚如何调用它。
But I can not figure it out how to call it.
问题!如何(用一个例子邮件
功能非常欢迎)来调用由 PHP_FUNCTION定义函数
?
The question! How (an example with mail
function is very welcomed) to call functions defined by PHP_FUNCTION
?
推荐答案
要找出一个功能是如何工作的最简单的方法是搜索它lxr.php.net。这变成了第一个例子是在readline的:http://lxr.php.net/xref/PHP_TRUNK/ext/readline/readline.c#474
The easiest way to figure out how a function works is to search for it on lxr.php.net. The first example that turns up is in readline: http://lxr.php.net/xref/PHP_TRUNK/ext/readline/readline.c#474
为邮件
的使用是相似的。鉴于参数作为变量容器( to_zval
, from_zval
, msg_zval
)调用非常简单:
The use for mail
is analogous. Given the arguments as zvals (to_zval
, from_zval
, msg_zval
) the call is very simple:
zval *params = { to_zval, from_zval, msg_zval };
zend_uint param_count = 3;
zval *retval_ptr;
zval function_name;
INIT_ZVAL(function_name);
ZVAL_STRING(&function_name, "mail", 1);
if (call_user_function(
CG(function_table), NULL /* no object */, &function_name,
retval_ptr, param_count, params TSRMLS_CC
) == SUCCESS
) {
/* do something with retval_ptr here if you like */
}
/* don't forget to free the zvals */
zval_ptr_dtor(&retval_ptr);
zval_dtor(&function_name);
如果您没有参数变量容器已经,那么你可以使用创建它们 MAKE_STD_ZVAL
和 ZVAL_STRING
If you don't have the parameters as zvals already, then you can create them using MAKE_STD_ZVAL
and ZVAL_STRING
.
这篇关于PHP。延期。呼叫现有的PHP函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!