Is there a way to override built in php functions without APD?

Posted on 16th Feb 2014 by admin

I am trying to use the
rename_function()
override_function()
options that are built into the APD php extension. But I don't want to rely on that extension being loaded.

This is another one of my out-of-the-box ideas that presents me with the thought to override the require_once function. During this function call, I want to check for the existence of the same file in another path. If it exists then require that one.. otherwise just do normal require once.

If I use the APD extension it works fine with this:

Code: rename_function('require_once', 'require_once_orig');
override_function('require_once', '$file', 'return require_once_new($file);');

function require_once_new($file) {
$modfile = str_replace(ROOT.'/', ROOT.'/'.MODS.'/', $file);
if (file_exists($modfile)) {
require_once_orig($modfile);
} else {
require_once_orig($file);
}
}

so if original file is:
/home/public_html/site/includes/page.php
I want to check if
/home/public_html/site/mods/includes/page.php exists first and load the one I want.

I am overriding the require_once function because I am adding this to an existing framework and don't want to have to update it in all locations. This would make it completely dynamic so that all require_once calls will check for existence of the file in the other path.

Any thoughts?

Other forums