override_function() in PHP
Saturday, November 29th, 2008I’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);
}


