php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #62518 Nested Includes
Submitted: 2012-07-09 23:04 UTC Modified: 2012-07-10 06:57 UTC
From: seregon at gmail dot com Assigned:
Status: Wont fix Package: *General Issues
PHP Version: 5.4.4 OS: Neutral
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: seregon at gmail dot com
New email:
PHP Version: OS:

 

 [2012-07-09 23:04 UTC] seregon at gmail dot com
Description:
------------
Scenario: File A includes File B, which in turn includes File C.

Problematic: The way PHP currently handles relative file paths (and this holds, from what I've seen, for include, require, file_get_contents, and just about everything else; I'm reasonably sure it's PHP's general relative file path handling I'm frustrated with) is by resolving everything with the path of the ROOT calling script. Thus, if A & B are in the same subdirectory, A can include B. If, however, A moves from the subdirectory, then A cannot include B if C has any relative file paths. This is because C will call them with A's path when A starts the chain of includes, and B's path when B starts it. Since A & B are not in the same directory, this will always fail for at least one of them.

Expected result:
----------------
I can't really make any sense of why it was done this way, and I haven't found anything in the documentation mentioning it. If this supports another worthwhile feature, then I'll gladly be educated. Further, I'm open to suggestions to deal with it. However, as I've been dealing with this the last few days, it's a very frustrating impediment to having a non-OO file in a single location with handy functions in it. You have to copy the damn thing all over the file system to have relative paths in it, which defeats the whole point of it having relative paths.

To me, this should proceed rather like functions (since it's by its nature procedural -- if File C was OO this would be moot). Function C's calls should have a sort of "local scope" whereby relative paths and perhaps other such things could be resolved, so that they can be used no matter where they reside in the file system without having to hardcode absolute paths (meaning they have to be updated when something else changes) or duplicate them across the fs (meaning they can be thrown out of sync and have to be updated multiple times).

Actual result:
--------------
as above

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-07-09 23:11 UTC] rasmus@php.net
Normally for commonly included files you would do:

include 'my_includes/file.php';

and your include_path contains whatever the parent of the my_includes dir is. 
That way that include works from everywhere. 

And yes, making includes relative to the executing script was done on purpose 
because it makes life much easier for a number of situations where you actually 
want location-specific includes such as theming/templating systems.
 [2012-07-09 23:11 UTC] rasmus@php.net
-Status: Open +Status: Wont fix
 [2012-07-10 06:57 UTC] rasmus@php.net
Oh, and just to clarify, if you don't want relative includes to check the script 
directory, just remove '.' from your include_path. It is there by default to 
make it easier for people, but we actually suggest just having a single 
include_path and not including '.' at all. If you want to always include 
relative to the directory of the including file, just use include 
__DIR__.'/file.php'; Basically all scenarios are possible when it comes to 
include/require.

At the start of a top-level script PHP does a chdir() to the directory of that 
running script, so yes, you will be running out of that directory. Doing 
multiple chdir() calls for every sub-include would be both slow and confusing, I 
think. But by using __DIR__ and include_path which some functions, including 
file_get_contents() can optionally use, you have plenty of flexibility.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue May 06 19:01:28 2025 UTC