1

I have a PHP code which includes several files if they exists. It might be 150 files to include.

Is it slow to use file_exists in this case? Should I build an array to create a cache of the file structure instead?

Is there other ways to make some kind of cache of the file structure?

6
  • 1
    can you explain more the origin of these included files? Is it classes? in this case a godd-written autoloader coudl help you, do you really need to include all these files? why are you unsure they're there? Commented Feb 22, 2011 at 11:50
  • I have a kind of subthemes, each subtheme is within it's own folder. Some files can be there by default but don't have to be. If they are, they should be loaded. Commented Feb 22, 2011 at 13:03
  • how many files with PHP or HTML code per one theme? Why don't you combine these files into one template? Commented Feb 22, 2011 at 13:20
  • It's an experiment with a dynamic theme where some parts of the theme can be used in another theme. Therefor I separate each folder / part very strict. I can combine some of the files with PHP, and I will. Commented Feb 22, 2011 at 14:30
  • is it php files or just HTML? if it's user-defined themes, I'd aviod php in it - so, no include would be used. Commented Feb 22, 2011 at 15:44

3 Answers 3

1

150 files during serving single request?
If so, you have to reduce this number, not because of 150 file_exists but because of 150 includes.

If it depends on the requested parameters and being 3-4 per request, it's OK.

You cannot use a regular array as a cache though. PHP doesn't preserve variables between requests. Instead of inventing such cache you have tho rethink your application structure.

3
  • well, I do not completly agree, if you look at a real Zend Framework project chances are that get near the 150 includes, but using apc and require_once optimizations settings in apc can make these includes really fast. Commented Feb 22, 2011 at 11:47
  • @regilero, actually it only includes a handful of files. It's uses an autoloader, so when you request a class ie. Zend_Form_Element, the autoloader knows by the class name where the to find the file on the file system and only then includes it. Commented Feb 22, 2011 at 12:20
  • @xzyfer: yes, but even with an autoloader some parts of real-life accounting ZF project I have seen are loading a huge number of files. and using apc behind the autoloader is really helping to decrease IO calls. Commented Feb 22, 2011 at 12:34
1

This is very bad idea. There are two proven ways around this.

  • Using an autoloader dynamically resolve a classes file location on the first instantiation usually requires a strict folder structure (ala Zend Framework)
  • recursively find all the relevent class files and generate a cached index of classname to location. So on the first instantiation you have a much simplier autoloader that looks at the cached index and match the file's location based on the classname and include it then (ala Symfony1 )
-3

@include($file)

include implicitly checks whether the file exists when it tries to open it.

Per comments below:

  • This turns off error checking in the entire included file.
  • Including a non-existant file is somewhat slower than including an existant file and significantly slower than a file_exists check.
12
  • 1
    Suppressing errors with @ is a massive performance hit. Doing this 150 times per request would be crippling to the server. Commented Feb 22, 2011 at 12:27
  • @Col, for starters check out the comments, php.net/manual/en/language.operators.errorcontrol.php. Many of similar claims are easy enough to find with a simple google search. If you really doubt the validity you're welcome to try it yourself and publish the findings for the rest of us. Commented Feb 22, 2011 at 12:32
  • @aaz, might be good to note the big red warning dialog on the manual page. "Currently the "@" error-control operator prefix will even disable error reporting for critical errors that will terminate script execution. Among other things, this means that if you use "@" to suppress errors from a certain function and either it isn't available or has been mistyped, the script will die right there with no indication as to why." Commented Feb 22, 2011 at 12:33
  • aaz, You're throwing out the baby with the bath water: to suppress just one kind of error, you're gagging all other possible error messages. And second "extremely" goes for using it with include, which will turn errors off in the whole included script Commented Feb 22, 2011 at 12:37
  • @xzyfer well you have no numbers but just heard some rumors. Nuff said. Just a friendly advise: don't be so unsuspecting. Commented Feb 22, 2011 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.