5

Is the following example appropriate for PHP's require_once construct?

function foo( $param )
{
    require_once "my_file.php" ;
    //
    // do something here
}

Or is it more appropriate to only have require_once constructs at the beginning of the file?

Even though the file being included is useful only in the context of the function, is it not better to have includes at the top for readability and maintainability?

Misha M
  • 10,979
  • 17
  • 53
  • 65

5 Answers5

8

It comes down to a matter of coding style and opinion. Personally I keep all my require_once statements at the very top of my files so I can easily see which files are included where, nothing worse then some buried include messing with your scripts. However, if you have several large required scripts that are only required for certain functions, then putting the require_once inside a function would be OK from a performance stand-point, just make sure to put a note at the top of the page.

<?php
//require_once "my_file.php" (see function foo)

function foo($param) {
  require_once "my_file.php";
}
TJ L
  • 23,914
  • 7
  • 59
  • 77
  • 3
    @tj111: A potential issue with commented out code like the snippet you have posted in your answer is that if some maintenance programmer updates `foo()` and removes the dependency on `my_file.php` they may not know to update the comment at the top of the file. The comment then hangs around and "lies" to every future programmer who looks at the file. – Asaph Nov 04 '09 at 22:03
  • @Asaph: I agree with what you, and personally wouldn't factor my code in the way I recommended for that very reason. I meant it as a kinda of fallback in case there are *serious* issues (e.g. performance) with including the file on every call, an issue which I personally have never had. – TJ L Nov 06 '09 at 18:57
4

This is something of a religious debate.

PROS for require and include statements at the top of the file:

  1. dependencies are clearly documented in a consistent reliable place.

  2. increased readability/maintainability

  3. OP code caching is simpler (although you could argue that this doesn't affect the developer directly)

CONS for require and include statements at the top of the file:

  1. If you're doing some kind of dynamic runtime including (such as with __autoload()), a hardcoded statement at the top of the file is impossible.

  2. If only one execution path in the code uses an include, having it included every time, unconditionally is a waste of resources.

  3. long list of include or require statement is just noise the developer must scroll past when editing a file. Of course, a long list of dependencies can be viewed as a sign that the code should be broken up into smaller more focused pieces, so maybe you could spin this one as a PRO because it makes a code smell stand out.

Asaph
  • 159,146
  • 25
  • 197
  • 199
3

If you don't want to load a file unless it's needed, look into autoloading - on newer PHP via spl_autoload_register().

gnud
  • 77,584
  • 5
  • 64
  • 78
1

Maybe you only need the included file in certain cases, and you'd like to avoid including it if you don't need it at all, if it's a big file. So, I guess you could go for a require_once only in one branch of an if - else statement.

luvieere
  • 37,065
  • 18
  • 127
  • 179
1

When using require_once keep in mind that this is not some pre-processor directive. The require_once statements are executed when PHP runs the code and it only executes if the specific script has not already been included during the execution.

For example:

conf.php:

<?php
$maxAge = 40;
?>

myscript.php

<?php
 function foo($age) {
  require_once("conf.php");
  if($age > $maxAge) 
    return "1"; 
  else 
    return "0";
}

 echo foo(30); // Echos 1
 echo foo(30); // Echos 0
?>

The require_once is not executed on the second call to foo(..) since conf.php has already been included once.

Juha Palomäki
  • 26,385
  • 2
  • 38
  • 43