【问题标题】:PHP extensions - call your own PHP function from another PHP functionPHP 扩展 - 从另一个 PHP 函数调用您自己的 PHP 函数
【发布时间】:2010-12-03 14:35:58
【问题描述】:

假设我们有一个自定义 PHP 扩展,例如:

PHP_RSHUTDOWN_FUNCTION(myextension)
{
   // How do I call myfunction() from here?
   return SUCCESS;
}
PHP_FUNCTION(myfunction)
{
   // Do something here
   ...
   RETURN_NULL;
}

如何从 RSHUTDOWN 处理程序调用 myfunction()?

【问题讨论】:

  • 你有没有想过这个问题?我试图想办法从自定义 ext 中调用 json_encode。功能,无法弄清楚如何将其关闭。
  • call_user_func('json_encode', $param);

标签: php c php-extension


【解决方案1】:

使用提供的宏调用将是:

PHP_RSHUTDOWN_FUNCTION(myextension)
{
   ZEND_FN(myFunction)(0, NULL, NULL, NULL, 0 TSRMLS_CC);
   return SUCCESS;
}

当你定义你的函数为PHP_FUNCTION(myFunction) 时,预处理器会将你的定义扩展为:

ZEND_FN(myFunction)(INTERNAL_FUNCTION_PARAMETERS)

这又是:

zif_myFunction(int ht, zval *return_value, zval **return_value_ptr, zval *this_ptr, int return_value_used TSRMLS_DC)

zend.h 和 php.h 中的宏:

#define PHP_FUNCTION            ZEND_FUNCTION
#define ZEND_FUNCTION(name)         ZEND_NAMED_FUNCTION(ZEND_FN(name))
#define ZEND_FN(name)                       zif_##name
#define ZEND_NAMED_FUNCTION(name)       void name(INTERNAL_FUNCTION_PARAMETERS)
#define INTERNAL_FUNCTION_PARAMETERS int ht, zval *return_value, zval **return_value_ptr, zval *this_ptr, int return_value_used TSRMLS_DC
#define INTERNAL_FUNCTION_PARAM_PASSTHRU ht, return_value, return_value_ptr, this_ptr, return_value_used TSRMLS_CC

【讨论】:

    【解决方案2】:

    你为什么不把 PHP_FUNCTION 变成这样的存根:

    void doStuff()
    {
      // Do something here
      ...
    }
    
    PHP_RSHUTDOWN_FUNCTION(myextension)
    {
       doStuff();
       return SUCCESS;
    }
    PHP_FUNCTION(myfunction)
    {
       doStuff();
       RETURN_NULL;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-19
      • 2012-06-30
      • 1970-01-01
      • 2013-08-10
      • 1970-01-01
      • 1970-01-01
      • 2015-07-09
      • 1970-01-01
      相关资源
      最近更新 更多