Req #62518 [Wfx]: Nested Includes
Edit report at https://bugs.php.net/bug.php?id=62518&edit=1
ID: 62518
Updated by: rasmus@php.net
Reported by: seregon at gmail dot com
Summary: Nested Includes
Status: Wont fix
Type: Feature/Change Request
Package: *General Issues
Operating System: Neutral
PHP Version: 5.4.4
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2012-07-09 23:11:20] 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:04:41] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=62518&edit=1
Thread (3 messages)