Is it possible to exclude '.' (i.e. current directory) from PHP include path?

From viewing 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 possible to get around this path?

In my work, I use my own switch and class loader, and I would like to control the behavior of PHP include (). My inclusion was used to provide absolute paths, but I think it is really too restrictive, and I would not want to return to this. I would like to work with PHP include_path, if at all possible.

+5
source share
2 answers

It's impossible. This is what the include () documentation says : "... include () will finally check its own directory and the current working directory in the calling script before the failure"

+3
source

Well, I am convinced.

The solution to my problem is iteration get_ini('include_path')for each included $fileName, conversion to absolute paths and process, respectively. Actually minimal changes in my regular class. The cool bootloader will not require any changes.

Thanks for your quick answers!

The following are the relevant updated methods from my includeer class: ($ this-> includePath is initialized by 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]);
    }
}
0
source

All Articles