tags:

views:

149

answers:

3

Assuming PHP version >= 5.2, which is a better solution for managing includes:

require_once DEFPATH . 'config.inc.php';

or

if (!defined('FOOBAR')) require DEFPATH . 'config.inc.php';

or something entirely different?

Site does not use a front controller or autoloader. Typical number of files needed to be included is 3 - 8.

I've heard, here and elsewhere, that require_once adds overhead and does not play nice with caching. But I've also heard that a small number of require_once statements is OK in practice in later versions of PHP.

There must also be some overhead associated with checking if something is defined, but that may be less of an issue.

In general, which is the better practice and why?

THANKS

+2  A: 

I would use require_once in your case. Even if it adds overhead, you can neglect it, especially when you only have to include max 8 files. The require_once is a lot less error prone and more 'clean'...

Fortega
A: 

In your example, checking if FOOBAR is defined would be better. But that's because your example is not specifying a full path to the include file.

You should always specify the full path for just about anything, unless you specifically want to use a relative path. Otherwise PHP needs to search in the various search path directories for your file. If it happens to be in the last search path directory, that's a bit of overhead.

I always create an array containing the full path to directories I load files from, then I prepend the appropriate path to the file I'm loading.

Brent Baisley
@brent-baisley I'll make an edit to the question for clarity, indicating the use of a defined path. Does your answer still hold if this is the case? Thanks.
Clayton
+3  A: 

Yes, require_once() comes with CPU and memory overhead, but not in any way significant to performance. In fact, it's a very efficient operation because PHP just does one hashtable lookup to decide whether that file has already been parsed or not. Don't give yourself any unnecessary headaches with hacks like your !defined('FOOBAR') example, because it does the same thing only with less elegance.

What takes time during a PHP request is the file path lookup and then the subsequent parsing step. The amount of resources needed for the runtime to determine whether you've already parsed the file before is negligible.

There are a few things you can do to make your request run faster:

  • avoid unecessarily long search path in include() and require()
  • use absolute include paths were possible
  • include stuff only as needed
  • cache large output fragments
  • cache complex query results
  • write terse code, offload seldom-used functionality to included-on-demand source files

If you're using a code cache or some kind of runtime optimizer, read the manual - they have different strategies that can actually hurt performance or introduce bugs depending on your code.

One final advice: beware premature optimization! Requests taking between 0 and 50 miliseconds on the server are practically not distinguishable on the user side.

Udo