在php中包含路径和__autoload函数

发布于 2024-09-10 23:02:58 字数 304 浏览 0 评论 0原文

我正在尝试转换几个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一指流沙 2024-09-17 23:02:58

不要使用 __autoload...它有一些缺点(包括限制每次执行一个)。如果您使用的是 5.2+,请改用 spl_autoload_register

所以我通常做的是有一个类:

class AutoLoader {
    protected static $paths = array(
        PATH_TO_LIBRARIES,
    );
    public static function addPath($path) {
        $path = realpath($path);
        if ($path) {
            self::$paths[] = $path;
        }
    }
    public static function load($class) {
        $classPath = $class; // Do whatever logic here
        foreach (self::$paths as $path) {
            if (is_file($path . $classPath)) {
                require_once $path . $classPath;
                return;
            }
        }
    }
}
spl_autoload_register(array('AutoLoader', 'load'));

这样,如果您添加一个库集,您可以通过调用 AutoLoader::AddPath($path); 将其“添加”到您的路径中。这使得使用自动加载器进行测试变得更加容易(恕我直言)。

另一张纸条。除非绝对必要,否则不要从自动加载类中抛出异常。原因是您可以安装多个自动加载器,因此如果您不知道如何加载该文件,则可能存在另一个自动加载器来加载它。但是如果你抛出一个异常,它会跳过另一个异常......

就我个人而言,我不喜欢在包含中使用相对路径。特别是对于多个包含目录(如 pear),当您看到 require 'foo/bar.php'; 时,很难准确地知道正在导入哪个文件。我更喜欢在文件集的开头定义绝对路径 define('PATH_ROOT', dirname(__FILE__));,然后定义该目录之外的所有其他有用路径 (PATH_LIBRARIESPATH_TEMPLATES 等...)。这样,一切都是绝对定义的......并且不需要处理相对路径(就像你现在遇到的问题)......

Don't use __autoload... It has a few drawbacks (including limiting yourself to one per execution). Use instead spl_autoload_register if you're on 5.2+.

So what I typically do, is have a class:

class AutoLoader {
    protected static $paths = array(
        PATH_TO_LIBRARIES,
    );
    public static function addPath($path) {
        $path = realpath($path);
        if ($path) {
            self::$paths[] = $path;
        }
    }
    public static function load($class) {
        $classPath = $class; // Do whatever logic here
        foreach (self::$paths as $path) {
            if (is_file($path . $classPath)) {
                require_once $path . $classPath;
                return;
            }
        }
    }
}
spl_autoload_register(array('AutoLoader', 'load'));

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 set define('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)...

ゝ偶尔ゞ 2024-09-17 23:02:58

我怀疑您的 __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.

深巷少女 2024-09-17 23:02:58

似乎 . 不在您的包含路径中。所以添加它:

set_include_path('.' . PATH_SEPARATOR . get_include_path());

现在 PHP 也应该相对于执行的脚本目录进行查找。 (这里执行的脚本类似于 index.php,而不是 autoload.php

但是为什么不简单地使用像 ./path/ 这样的普通相对路径到/class.php

It seems like . is not in your include path. So add it use:

set_include_path('.' . PATH_SEPARATOR . get_include_path());

Now PHP should look relative to the executed scripts directory, too. (Executed script here is something like index.php, not autoload.php.

But why don't use simply use a normal relative path like ./path/to/class.php?

两人的回忆 2024-09-17 23:02:58

没有看到整个设置就不确定。我的自动加载函数位于我的全局函数文件中,如下所示:

function __autoload($class) {
    if (file_exists("includes/{$class}.php")) {
        require_once("includes/{$class}.php");
    }
    /**
     * Add any additional directories to search in within an else if statement here
     */
    else {
        // handle error gracefully
    }
}

我使用相对路径,因为该脚本包含在我的 index.php 文件中,并且所有 HTTP 请求都通过它传递。

Not sure without seeing the whole set-up. My autoload function is within my global functions file, and looks like this:

function __autoload($class) {
    if (file_exists("includes/{$class}.php")) {
        require_once("includes/{$class}.php");
    }
    /**
     * Add any additional directories to search in within an else if statement here
     */
    else {
        // handle error gracefully
    }
}

I use a relative path because the script is included in my index.php file and all HTTP requests are passed through it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文