Just out of curiosity I wrote a quick script to determine how many times I references files through out my source...
41 matches.
All I need to do is rename that file once or relocate the file in a subfolder for better organization and snap broken code in 41 places...
I have quickly came up with the following solution to abstract file paths:
Code: Select all
//
// NOTE: Use these constants to index path using varpath()
//
define('CACHE_TPL', 0);
define('CACHE_HTML', 1);
//
// NOTE: All paths are relative to root directory
//
$GLOBALS['varpaths'][CACHE_HTML] = 'var/cache/html/%p0.html';
$GLOBALS['varpaths'][CACHE_TPL] = 'var/cache/tpl/%p0.tpl';
function varpath($index, $params = array())
{
$path = $GLOBALS['varpaths'][$index];
foreach($params as $offset => $param){
$param = is_int($param) ? (int)$param : preg_replace('/[^a-z0-9]/', '', $param);
$path = str_replace("%p$offset", $param, $path);
}
return $path;
}
The instead of having hardcoded paths scattered all over the place I do something like:
Code: Select all
echo file_get_contents(varpath(CACHE_HTML, array('index')));
index would probably be best left static instead of a placeholder but frequently I look up files specific to users, etc in which case the dynamic replacement becomes nesseccary.
Can you see any issues with this approach? What techniques do you use to prevent hardcoded paths from taking over your codebase?
Cheers,
Alex