From perusing the comments at
http://php.net/manual/en/function.set-include-path.php , it seems to me that '.', or rather basename(__FILE__)
, is always implicitly added to PHP's include path. Is it at all possible to bypass this path?
In my work I'm using my own includer and class loader and I'd like to control the behavior of PHP's include(). My includer used to enforce absolute paths but I think this is really too restrictive, and I wouldn't want to revert to that. I'd like to work with PHP's include_path, if at all po开发者_运维百科ssible.
It's not possible. Says so in the documentation of include(): "... include() will finally check in the calling script's own directory and the current working directory before failing"
Okay, I'm convinced.
The solution to my problem is to iterate get_ini('include_path')
for each included $fileName
, convert to absolute paths and process accordingly. Minimal changes to my custom include class, really. The class loader will not require any changes.
Thanks for your prompt answers!
Below are the relevant updated methods from my includer class: ( $this->includePath is initialized to get_ini('include_path') )
// Pre-condition for includeFile()
// checks if $fileName exists in the include path
public function mayIncludeFile($fileName)
{
if(array_key_exists($fileName, $this->includeMap))
{
return TRUE;
}
if($fileName{0} == DIRECTORY_SEPARATOR)
{
if(is_file($fileName))
{
$this->includeMap[$fileName] = $fileName;
return TRUE;
}
}
else foreach($this->includePath as $index => $path)
{
$absoluteFileName = $path . DIRECTORY_SEPARATOR . $fileName;
if(is_file($absoluteFileName))
{
$this->includeMap[$fileName] = $absoluteFileName;
return TRUE;
}
}
return FALSE;
}
public function includeFile($fileName)
{
$this->validateFileName($fileName, TRUE);
if((array_key_exists($fileName, $this->includeMap) && $this->includeMap[$fileName]) ||
$this->mayIncludeFile($fileName))
{
include_once($this->includeMap[$fileName]);
}
}
精彩评论