1

As helpfully stated in the documentation for include():

If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing.

Is there any setting to prevent this and make it fail in some way instead? I want to know when my code "thinks" files should be in places they aren't. (Or can I at least add a warning when it happens?)

Is my only option to use file_exists()? That feels wrong somehow; I don't want to roll my own include function.

  • There's no way to change the behavior of the default PHP function. You'd either have to write your own type of include function or check with something like file_exists(). – versalle88 Jun 15 '15 at 16:40
  • Okay. It seems a little odd that it has to be that way. I'll just live with the possibility that my code is "technically wrong" about these things but works fine anyway. Should I delete/flag this question? – Chris Morrow Jun 15 '15 at 16:43
  • Can I ask why you're using include/require anyway? Why not use an autoloader? – DanielM Jun 15 '15 at 16:44
  • I guess I thought an autoloader would complicate things, or just be "wrong" to rely on. – Chris Morrow Jun 16 '15 at 12:46

1 Answers1

1

Hold on, all you have to do is provide a file path yourself if you don't want to have the default "helpful" behavior applied.

From the PHP manual...

Files are included based on the file path given or, if none is given, the include_path specified.

and

If a path is defined — whether absolute (starting with a drive letter or \ on Windows, or / on Unix/Linux systems) or relative to the current directory (starting with . or ..) — the include_path will be ignored altogether. For example, if a filename begins with ../, the parser will look in the parent directory to find the requested file.

A Smith
  • 621
  • 1
  • 4
  • 10
  • Maybe I wasn't clear, or I'm misunderstanding you. I'm talking about the behavior even when a path is specified, but no file is found there. – Chris Morrow Jun 16 '15 at 12:58
  • As far as I can tell the fallback behavior only happens if you DO NOT provide a path of your own. So, you can give a path such as require_once("./filename.php") and not have to worry about any fallback involving whatever include path may be defined. To avoid fallback always provide your own explicit path details. – A Smith Jun 16 '15 at 16:54
  • Ah, that seems correct. My experience may be affected by my using the set_include_path function, which I should have mentioned in the original question but thought it wasn't a relevant detail. – Chris Morrow Jun 16 '15 at 17:20