override_function() in PHP

I’ve been writing a wrapper for Cherokee’s MySQL load balancer to transparently replace PHP’s MySQL functions and provide connection pooling and database balancing at absolutely no cost. And I encountered a problem wanting to override those built-in functions.

It looks like the override_function() has a bug that prevents from using it to override two or more functions, since it issues this error:

Fatal error: Cannot redeclare __overridden__()

Judging by the C code for PHP’s override_function() function, instead of hardcoding the overriden function name it was planed to generate random names, but for reasons unknown to me this never happened.

Anyway, I can’t believe the fix for this is so simple and yet the solution didn’t appear in PHP’s excellent online documentation. It had me lost for a while, but it’s as simple as renaming the overriden function!! This is from the wrapper I mentioned, that will be distributed with Cherokee shortly:

foreach ($substs as $func => $ren_func) {
    override_function($func, $args[$func], "return $substs[$func];");
    rename_function("__overridden__", $ren_func);
}

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

2 Comments »

 
 

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>