php 嵌套包含行为
在我的代码中的许多地方,我会执行以下操作:
file1.php:
<?php
include('../file2.php');
file2.php:
<?php
include('anotherdirectory/file3.php');
根据我尝试执行此操作的服务器或设置,它会设置来自“includer”或“includee”的相对路径。这确实令人困惑。 因此 file1 可能会尝试包含“../anotherdirectory/file3.php”,或者可能会尝试“anotherdirectory/file3.php”。
什么设置决定了这种行为?我想控制这个...
In many places in my code, I do things like:
file1.php:
<?php
include('../file2.php');
file2.php:
<?php
include('anotherdirectory/file3.php');
Depending on the server or settings I try this on, it either sets the relative paths from the "includer" or from the "includee". This is really confusing.
So file1 might try to include "../anotherdirectory/file3.php" or it might try "anotherdirectory/file3.php".
What settings dictate this behavior? I want to have control over this...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果我需要使用相对路径,我使用以下语法:
In cases when I need to use relative paths I use the following syntax:
我建议使用绝对路径。在保持可移植性的同时做到这一点的一个好方法是在 public_html/index.php 中进行如下声明:
然后,您可以像这样编写非常简单的包含:
否则,PHP 会检查该文件是否在包含路径 由 php.ini 定义。如果不存在,它将尝试当前脚本的相对路径。这是不可预测且不可维护的,因为您可能嵌套地包含来自不同相对位置的文件。
编辑:如果您经常包含大量类文件,您可能需要研究自动加载。如果您以面向对象的风格进行编程,它会让一切变得更简单。我个人已经很长时间没有在我的代码中写过“include”这个词了。
编辑2:您可以使用 php.ini 指令
auto_prepend_file 自动将包含 ROOT 定义的一行文件包含到每个脚本中。
I would recommend using absolute paths. A good way to do this while still being portable is to make a declaration like this in your public_html/index.php:
Then, you can write includes like this which are very easy:
Otherwise, PHP checks to see if the file is in the include path as defined by your php.ini. If it's not there, it tries a relative path to the current script. Which is unpredictable and unmaintainable since you may be nestingly including files from different relative locations.
Edit: If you're constantly including a lot of class files, you may want to look into autoloading. It makes everything way simpler if you're programming in an object-oriented style. I have personally never written the word 'include' in my code for a very long time.
Edit 2: You could use the php.ini directive
auto_prepend_file
to automatically include a one-line file with the definition of ROOT to each one of your scripts.作为 php 学习曲线上的人,我发现引用包含路径的最佳方法是通过绝对位置,而不是相对位置,通过使用内置的
$_SERVER
超全局。在我自己的文件中,我已经成功地使用了它:这样,包含的文件相对于我的调用文件所在的位置并不重要,而且我不必担心手动输入完全限定的服务器路径。 (也许是显而易见的..)例如,无论包含调用如何嵌套,以及如果/当我将调用文件移动到不同的目录时,这都会起作用。
您可以将此方法与
include
、require
以及任何其他需要路径的文件相关函数结合使用。相关说明..
将返回当前文件的路径(相对于根目录)。我也经常使用这个。
$_SERVER
还有其他有用的信息,您可能想在这里查看:http://php.net/manual/en/reserved.variables.server.php
抱歉,如果这是旧线程,我是新来的。
编辑:您可以将此
'DOCUMENT_ROOT'
保存到变量中以供以后使用,但根据最近的经验,我建议不要这样做,因为那样您就必须担心变量范围。无论当前范围如何,所编写的包含行每次都会起作用。As someone on the php learning curve, I have found the best way to reference include paths is by absolute location, not relative, by using the built-in
$_SERVER
superglobal. In my own files I have been using this with success:This way it doesn't matter where the included file resides relative to my calling file, and I don't have to worry about manually typing in my fully qualified server path. (Maybe obvious..) This will work no matter how nested the include call is, and if / when I move the calling file to a different directory, for example.
You can use this method with
include
,require
, and any other file-related functions that need a path.On a related note..
will return the path (relative to the root) of the current file. I also use this quite a bit.
$_SERVER
has other useful info you may want to check out here:http://php.net/manual/en/reserved.variables.server.php
Sorry if this is an older thread, I'm new here.
EDIT: You could save this
'DOCUMENT_ROOT'
to a variable for use later, but from recent experience I would recommend against it because then you have to worry about variable scope. The include line as written will work every time regardless of current scope.使用 get_include_path() 您可以看到服务器配置是什么。在大多数情况下,它看起来像这样:
这意味着,php 寻找包含文件的第一个位置是包含另一个文件的脚本的目录。如果那里不存在,则 php 正在 /usr/php/lib 中查找。如果添加更多路径,php 也会在那里查找匹配的文件。
如果您包含一个文件,其中包含另一个文件,则“根”路径是最初包含另一个文件的文件的路径。
With
get_include_path()
you can see, what the server configuration for this is. In most cases it looks like this:This means, the first place php is looking for a included file is the directory of the script that includes another. If it is not present there, php is looking in /usr/php/lib. If you add more paths, php will also look there for a matching file.
If you include a file, which includes another one, the "root" path is the path of the file which included another one at first.