How to overload functions from a PHP extension?

644 Views Asked by At

I've been going through some PHP extension tutorials, but I can't find any information about how to overload existing function.

For example, I want to change the fopen() to something like

PHP_FUNCTION(fopen)
{
    if condition_is_true(condition)
        original_fopen();
    else
        show_error();
}

How could I do that? Thank you

PS. I mean the extension (written in C, compiled to .so and included in httpd.conf, not the .php program)


Edit: Found a solution, thanks to Gordon links.

I have downloaded PECL package for function rename_function. Its source code has led me to the required conclusions:

  • There is global hash table function_table, which holds all the pointers for functions, based on their names.

  • zend_hash_find/zend_hash_add/zend_hash_del will allow me to do whatever changes I want in this table.

1

There are 1 best solutions below

1
On

If you are using Zend Framework then all requests go through the same bootstrap file (usually index.php in your public directory)

You can use this to create a new php file called my_global_functions.php and include it like

require_once "my_global_functions.php"

now just create a new function there like

function my_fopen(){ 
    if condition_is_true(condition)
        fopen();
    else
        show_error();        
}

you should be able to call my_fopen from anywhere in your code now.