在php中包含路径和__autoload函数
我正在尝试转换几个 php 脚本以使用 __autoload 函数。现在我可以使用这样的 include 和 require 函数:
require_once('path/to/script.php');
但在 __autoload 函数内部,我无法使用上面的行。我必须使用这个:
require_once('absolute/path/to/script.php');
为什么 __autoload 函数似乎没有使用我在 php.ini 中指定的包含路径?
I am trying to convert several php scripts to use the __autoload function. Right now I can use the include and require functions like this:
require_once('path/to/script.php');
But inside of the __autoload function, I can't use the line above. I have to use this:
require_once('absolute/path/to/script.php');
Why does it seem as though the __autoload function doesn't use the include path I have specified in php.ini?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要使用
__autoload
...它有一些缺点(包括限制每次执行一个)。如果您使用的是 5.2+,请改用spl_autoload_register
。所以我通常做的是有一个类:
这样,如果您添加一个库集,您可以通过调用
AutoLoader::AddPath($path);
将其“添加”到您的路径中。这使得使用自动加载器进行测试变得更加容易(恕我直言)。另一张纸条。除非绝对必要,否则不要从自动加载类中抛出异常。原因是您可以安装多个自动加载器,因此如果您不知道如何加载该文件,则可能存在另一个自动加载器来加载它。但是如果你抛出一个异常,它会跳过另一个异常......
就我个人而言,我不喜欢在包含中使用相对路径。特别是对于多个包含目录(如 pear),当您看到
require 'foo/bar.php';
时,很难准确地知道正在导入哪个文件。我更喜欢在文件集的开头定义绝对路径define('PATH_ROOT', dirname(__FILE__));
,然后定义该目录之外的所有其他有用路径 (PATH_LIBRARIES
、PATH_TEMPLATES
等...)。这样,一切都是绝对定义的......并且不需要处理相对路径(就像你现在遇到的问题)......Don't use
__autoload
... It has a few drawbacks (including limiting yourself to one per execution). Use insteadspl_autoload_register
if you're on 5.2+.So what I typically do, is have a class:
That way, if you add a library set, you can just "add it" to your paths by calling
AutoLoader::AddPath($path);
. This makes testing with your autoloader a LOT easier (IMHO).One other note. Don't throw exceptions from the autoload class unless absolutely necessary. The reason is that you can install multiple autoloaders, so if you don't know how to load the file, another one may exist to load it. But if you throw an exception, it'll skip the other one...
Personally, I don't ever like to use relative paths with includes. Especially with multiple include directories (like pear), it makes it very difficult to know exactly which file is being imported when you see
require 'foo/bar.php';
. I prefer to define the absolute path in the beginning of the file setdefine('PATH_ROOT', dirname(__FILE__));
, and then define all my other useful paths off of that directory (PATH_LIBRARIES
,PATH_TEMPLATES
, etc...). That way, everything is absolutely defined... And no need to deal with relative paths (like the issue you're having now)...我怀疑您的 __autoload() 函数位于一个单独的文件中,然后是调用它的代码。所包含文件的路径将相对于
__autoload()
函数声明所在的文件。I suspect your
__autoload()
function is in a separate file then the code which calls it. The path to the included files will be relative to the file which the__autoload()
function declaration resides.似乎
.
不在您的包含路径中。所以添加它:现在 PHP 也应该相对于执行的脚本目录进行查找。 (这里执行的脚本类似于
index.php
,而不是autoload.php
。但是为什么不简单地使用像
./path/ 这样的普通相对路径到/class.php
?It seems like
.
is not in your include path. So add it use:Now PHP should look relative to the executed scripts directory, too. (Executed script here is something like
index.php
, notautoload.php
.But why don't use simply use a normal relative path like
./path/to/class.php
?没有看到整个设置就不确定。我的自动加载函数位于我的全局函数文件中,如下所示:
我使用相对路径,因为该脚本包含在我的
index.php
文件中,并且所有 HTTP 请求都通过它传递。Not sure without seeing the whole set-up. My autoload function is within my global functions file, and looks like this:
I use a relative path because the script is included in my
index.php
file and all HTTP requests are passed through it.