Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用PHP扩展覆盖内部Zend函数?_Php_Php Internals - Fatal编程技术网

如何用PHP扩展覆盖内部Zend函数?

如何用PHP扩展覆盖内部Zend函数?,php,php-internals,Php,Php Internals,希望这不是一个愚蠢的问题。我对C和PHP的内部结构相当陌生,但希望了解更多 我最近开始研究PHP扩展的开发。我试图在内部函数执行原始函数之前覆盖它以执行某些操作 我找到了一个覆盖var_dump()函数的示例。虽然我试图在此基础上构建并编译它,但似乎无法使其正常工作 以下是我目前拥有的代码: php_hook.c: // include the PHP API itself #include <php.h> // then include the header of your ext

希望这不是一个愚蠢的问题。我对C和PHP的内部结构相当陌生,但希望了解更多

我最近开始研究PHP扩展的开发。我试图在内部函数执行原始函数之前覆盖它以执行某些操作

我找到了一个覆盖var_dump()函数的示例。虽然我试图在此基础上构建并编译它,但似乎无法使其正常工作

以下是我目前拥有的代码:

php_hook.c:

// include the PHP API itself
#include <php.h>
// then include the header of your extension
#include "php_hook.h"

// register our function to the PHP API 
// so that PHP knows, which functions are in this module
zend_function_entry hook_php_functions[] = {
    ZEND_NAMED_FE(my_overwrite_var_dump, my_overwrite_var_dump, NULL)
    {NULL, NULL, NULL}
};

// some pieces of information about our module
zend_module_entry hook_php_module_entry = {
    STANDARD_MODULE_HEADER,
    PHP_HOOK_EXTNAME,
    hook_php_functions,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    PHP_HOOK_VERSION,
    STANDARD_MODULE_PROPERTIES
};

// use a macro to output additional C code, to make ext dynamically loadable
ZEND_GET_MODULE(hook_php)

#if PHP_VERSION_ID < 70200
typedef void (*zif_handler)(INTERNAL_FUNCTION_PARAMETERS);
#endif
zif_handler original_handler_var_dump;

ZEND_NAMED_FUNCTION(my_overwrite_var_dump)
{
    php_printf("hooked (var_dump))\n");
    // if we want to call the original function
    original_handler_var_dump(INTERNAL_FUNCTION_PARAM_PASSTHRU);
}

PHP_MINIT_FUNCTION(hook_php)
{
    zend_function *original;

    original = zend_hash_str_find_ptr(EG(function_table), "var_dump", sizeof("var_dump")-1);

    if (original != NULL) {
        original_handler_var_dump = original->internal_function.handler;
        original->internal_function.handler = my_overwrite_var_dump;
    }
}
php_hook.h:

// we define Module constants
#define PHP_HOOK_EXTNAME "php_hook"
#define PHP_HOOK_VERSION "0.0.1"

// then we declare the function to be exported
ZEND_NAMED_FUNCTION(my_overwrite_var_dump);

当我调用
var\u dump(1)
时,它只返回
int(1)
still。我目前使用的是PHP7.3。

我还没有测试过它,但我认为类似的东西应该可以工作:

...

PHP_FUNCTION(my_overwrite_var_dump)
{
    php_printf("hooked (var_dump))\n");
    // if we want to call the original function
    original_handler_var_dump(INTERNAL_FUNCTION_PARAM_PASSTHRU);
}


PHP_MINIT_FUNCTION(hook_php)
{
    zend_function *original;

    original = zend_hash_str_find_ptr(CG(function_table), "var_dump", sizeof("var_dump")-1);

    if (original != NULL) {
        original_handler_var_dump = original->internal_function.handler;
        original->internal_function.handler = PHP_FN(my_overwrite_var_dump);
    }
}
我改变的事情:

  • 我使用了
    PHP\u函数
  • 我用了
    CG()
  • 最后一点是,为了覆盖处理程序,必须使用
    PHP\u FN(my\u overwrite\u var\u dump)

让我知道进展如何

我还没有测试过它,但我认为类似的东西应该可以工作:

...

PHP_FUNCTION(my_overwrite_var_dump)
{
    php_printf("hooked (var_dump))\n");
    // if we want to call the original function
    original_handler_var_dump(INTERNAL_FUNCTION_PARAM_PASSTHRU);
}


PHP_MINIT_FUNCTION(hook_php)
{
    zend_function *original;

    original = zend_hash_str_find_ptr(CG(function_table), "var_dump", sizeof("var_dump")-1);

    if (original != NULL) {
        original_handler_var_dump = original->internal_function.handler;
        original->internal_function.handler = PHP_FN(my_overwrite_var_dump);
    }
}
我改变的事情:

  • 我使用了
    PHP\u函数
  • 我用了
    CG()
  • 最后一点是,为了覆盖处理程序,必须使用
    PHP\u FN(my\u overwrite\u var\u dump)
让我知道进展如何