I have a file I use called fileCabinet.php to load in all my classes etc. It works great when my site is loaded from index.php via:
require_once('../includes/fileCabinet.php');
My issue is when I make Ajax calls, I want to use the same classes and would love to use the same file to load them, but the location of the ajax handling script is different from the location of the index.php so I end up with something that looks like this:
if($admin){
$path = '../';
}else if($ajax){
$path = '../../';
}
@require_once($path . 'admin/includes/generalFunctions.php');
@require_once($path . 'admin/corModules/PageAdmin.php');
@require_once($path . 'admin/corModules/SEOManager.php');
I have two different paths depending on where I’m calling from (admin or ajax). I’m wondering if there is a die-hard approach to locating your files no matter where the call for it comes from.
Thoughts?
Inside
fileCabinate.phpinclude relative to that file only:The
__DIR__magic constant is the directory of the current file, so in this case it’s assumed the three sub files are in the admin sub-folder.Per op’s point – prior to 5.3, you needed to use:
… oh how quickly we forget how it worked in the good ol’ days!