PHP:当我从另一个包含文件的目录中包含一个文件时,为什么会出现这种奇怪的行为? [英] PHP: Why such weird behaviour when I include a file from another directory which also includes a file?

查看:120
本文介绍了PHP:当我从另一个包含文件的目录中包含一个文件时,为什么会出现这种奇怪的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从未见过这样的事情。

I've never seen anything like this.

文件结构:


  • somefolder / base.php

  • includes / stuff.php

  • includes / constants.php

base.php:

<?php require("../includes/stuff.php"); ?>

stuff.php:

<?php 
   require("constants.php"); 
   echo "Welcome to stuff.php";
?>

constants.php:

<?php echo "IMPORTANT CONSTANTS DEFINED HERE!";  ?>

结果:


欢迎来到stuff.php

Welcome to stuff.php

为什么不显示constant.php消息? !我会告诉你什么不是问题:它不是php代码。

Why doesn't it display the constant.php message?! I'll tell you what isn't the problem: it isn't the php code.

我从一些复杂的文件开始,其中常量文件实际上是DEFINE() - 一些常量,并且由于某种原因,只有前几个常量才有效。我最终禁用了所有无关的代码,包括DEFINE()和只有echo语句,这个问题仍然一直盯着我。顺便说一下,无论代码的顺序如何,一些已定义的常量如何通过而不是任何回声?

I started off with some complex files where the constants file was actually DEFINE()-ing some constants and for some reason the only the first few constants were working. I ended up disabling all the extraneous code including the DEFINE()'s and just having echo statements and this problem still kept staring me in the face. And by the way how could some of the defined constants make it through but not any echo's, regardless of the order of the code?

所以我终于意识到这是荒谬的决定只创建一些新文件作为测试。那些工作。或者也许他们没有,我忘了。无论如何,我已经通过创建新文件并尝试移动它们并将代码从一个文件复制到另一个文件而获得了一定程度的成功,但似乎没有任何押韵或理由。

So I finally realized this was absurd and decided to just create some new files as a test. Those ones worked. Or maybe they didn't, I forget. Anyway I have had some measure of success by creating new files and experimenting with moving them around and copying code from one file to another but there hardly seems to be any rhyme or reason to it.

哇,这就是:我让constants.php文件以不同的名称工作但是当我把它改成constants.php时它停止了工作!我尝试在不同的浏览器中清除缓存/打开并重新启动服务器,但没有一个有用。我觉得我或我的电脑都是毒品。 Gaaaahh !!!

Wow, this just in: I got the constants.php file working under a different name but when I changed it to constants.php it stopped working! I tried clearing the cache/opening in a different browser and restarting the server but none of it helps. I feel like either I or my computer is on drugs. Gaaaahh!!!

啊哈。我遗漏了一条非常重要的信息。我没有提到的一个文件。

Aha. I left out one very important piece of information it looks like. One file I didn't mention.


  • somefolder / constants.php

包含该文件而不是includes / constants.php。这就是我没有丢失文件错误的原因。这完全令人困惑!我期望sub-includes在更高级别包含之前发生所以stuff.php会在它自己的目录中查找constants.php但是我猜base.php包含stuff.php而THEN base.php执行stuff.php中指定的include这意味着include是相对于base.php。所以绝对路径毕竟是解决方案。

That file was being included instead of the includes/constants.php. That's why I wasn't getting any missing file errors. That was totally confusing things! I expected that sub-includes happen before higher level includes so stuff.php would look for constants.php in it's own directory but I guess base.php includes stuff.php and THEN base.php does the include specified in stuff.php which means the include is relative to base.php. So absolute paths is the solution after all.

其他编辑:

非常有趣。当我简单地改变include / constants.php(例如constanza.php)的名字时,我仍然感到困惑。看起来文件包含不是那么剪切和干燥。如果有两个具有相同名称的文件,则它将优先选择相对于执行包含的父文件的文件。但是如果它没有选择它会相对于孩子做得很好。

Very interesting. I was still puzzled why it worked before when I simply changed the name of includes/constants.php (to constanza.php for example). It looks like file inclusion isn't so cut and dry. If there are two files with the same name it will prefer the one relative to the parent file doing the including. But if it has no choice it will do it relative to the child just fine.

推荐答案

你的包含中使用的路径不是相对于文件,包括:它们是相对于 include_path

The path used in your includes are not relative to the file the includes are : they are relative to the include_path.

这意味着使用包含/需要的相对路径通常不会对你做什么期待它... ...

Which means that using relative paths with includes/requires will generally not do what you expect it to...



通常使用的解决方案是使用绝对路径;通过这种方式,您可以确定包含哪些内容。


The solution that's generally used is to work with absolute paths ; this way, you are sure what gets included.

当然,在代码中使用绝对路径是错误的(它可以在一台服务器上运行,但不能在另一台服务器上运行,当您的应用程序部署到另一条路径时);因此,解决方案是:

Of course, using absolute paths in your code is bad (it will work on one server, but not another, when your application is deployed to another path) ; so, the solution is to :


  • 获取您所在文件的绝对路径

  • 除了另一个文件的相对路径外,还要包含该文件。



您可以使用 __ FILE __ 获取您所在文件的完整路径。


The full path to the file you're in can be obtained with __FILE__.

这意味着当前的目录文件(写入指令的文件)可以使用 dirname(__ FILE __)获得。

Which means the directory to the current file (the file that instruction is written in) can be obtained with dirname(__FILE__).

之后,只需编写正确的路径即可。

例如,在 stuff.php 中,您将使用:

After that, it's only a matter of writing the correct paths.
For example, in stuff.php, you'd use :

require(dirname(__FILE__) . "/constants.php"); 

base.php 中,你'使用:

require(dirname(__FILE__) . "/../includes/stuff.php");

这篇关于PHP:当我从另一个包含文件的目录中包含一个文件时,为什么会出现这种奇怪的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆