user2328799
user2328799

Reputation:

Trying to include a file, file_exists is essential?

Sorry for possiblely reduplicate asking. But it's hard to use this confusable keyword to search a answer.

So here is scenario:

I try to get a advise to make my simple autoloader. Here is i made so far:

private function getAutoInclude($classfile) {
    $classfileLower = strtolower($classfile);

    if (isset($this->configs['Paths']['base.'.$classfileLower])) { // Use path scope to locate file first
        return require_once($this->configs['Paths']['base.'.$classfileLower]['Path']);
    } elseif ($this->configs['LibRoot'] && strpos($classfile, '\\') !== false) { // If above not work, use namespace to locate file
        return require_once($this->configs['LibRoot'] . DIRECTORY_SEPARATOR . str_replace(array('\\', '/', '_'), DIRECTORY_SEPARATOR, ltrim($classfile, '\\')) . '.php');
    }

    return false;
}

It works well so far but only thing confused me is, some people tells me i must to do a file_exists check on the file i'm including so i can include it more safer AND more faster.

So consider the file i want to included must be there each time i include it, will i really have to file_exists in this scenario or not?

(I know this question just like a newbie asked. But when i heard people say if file_exists can make file include faster, it break some of my knowledge on PHP.)

Upvotes: 1

Views: 83

Answers (2)

Seraphin Ahmed
Seraphin Ahmed

Reputation: 29

just use the __autoload() function and save yourself the stress because if those files you're requiring are nonexistence then you're i trouble of getting those red table error report

Upvotes: 0

Brad
Brad

Reputation: 163232

The file you are including is required. (Presumably anyway... you are using require_once().) If the file isn't there, your script will fail. No need for all this checking and nonsense, just do the require_once() call.

Also, add your paths to the include path. Let PHP figure it out. No need to write code to search for a file.

Upvotes: 1

Related Questions