PHP 自动加载器和 Unix 区分大小写的文件系统

发布于 2024-10-14 16:26:27 字数 1644 浏览 4 评论 0原文

我有 index.php

<?php
    include('_controller/Autoloader.php');
    Gold_Autoloader::init();

    $mysql = new Gold_MySQL();

_controller/Autoloader.php

<?php
class Gold_Autoloader
{

    public static $loader;

    public static function init()
    {
        if (self::$loader == NULL)
            self::$loader = new self();

        return self::$loader;
    }

    public function __construct()
    {
        spl_autoload_register(array($this, 'controller'));
        spl_autoload_register(array($this, 'resources'));
    }

    public function resources($className)
    {
        $className = preg_replace('#Gold_#', '', $className);
        $className = preg_replace('#_#', DIRECTORY_SEPARATOR, $className);

        set_include_path(PROJECT_ROOT . '_resources');
        spl_autoload_extensions('.php');
        spl_autoload($className);
    }

    public function controller($className)
    {
        $className = preg_replace('#Gold_#', '', $className);
        $className = preg_replace('#_#', DIRECTORY_SEPARATOR, $className);

        set_include_path(PROJECT_ROOT . '_controller');
        spl_autoload_extensions('.php');
        spl_autoload($className);
    }
}

并且我有带有 Gold_MySQL.class 的文件 _controller/MySQL.php 。 在 Windows 系统上,此代码可以工作,包括 MySQL.php,但在托管此代码时不起作用((

[Thu Jan 27 12:55:57 2011] [error] PHP Fatal error: Class 'Gold_MySQL' not found in /home/u91167/youd0main.com/www/index_.php on line 5

编辑

如何使 Unix 可以查看任何文件?Zend 没有带有小写字母的文件。

I have index.php

<?php
    include('_controller/Autoloader.php');
    Gold_Autoloader::init();

    $mysql = new Gold_MySQL();

_controller/Autoloader.php

<?php
class Gold_Autoloader
{

    public static $loader;

    public static function init()
    {
        if (self::$loader == NULL)
            self::$loader = new self();

        return self::$loader;
    }

    public function __construct()
    {
        spl_autoload_register(array($this, 'controller'));
        spl_autoload_register(array($this, 'resources'));
    }

    public function resources($className)
    {
        $className = preg_replace('#Gold_#', '', $className);
        $className = preg_replace('#_#', DIRECTORY_SEPARATOR, $className);

        set_include_path(PROJECT_ROOT . '_resources');
        spl_autoload_extensions('.php');
        spl_autoload($className);
    }

    public function controller($className)
    {
        $className = preg_replace('#Gold_#', '', $className);
        $className = preg_replace('#_#', DIRECTORY_SEPARATOR, $className);

        set_include_path(PROJECT_ROOT . '_controller');
        spl_autoload_extensions('.php');
        spl_autoload($className);
    }
}

And I have file _controller/MySQL.php with Gold_MySQL.class.
On windows system this code is working and including MySQL.php, but on hosting this code not working ((

[Thu Jan 27 12:55:57 2011] [error] PHP Fatal error: Class 'Gold_MySQL' not found in /home/u91167/youd0main.com/www/index_.php on line 5

EDIT

How to make so that Unix could look at any files? Zend has no files with a lowercase letter.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

痴者 2024-10-21 16:26:27

听起来您的文件名区分大小写,并且您尝试以错误的大小写加载文件。即您可能需要 gold_mysql.php 而不是 Gold_MySql.php

Sounds like your filename is case sensitive and you're trying to load a file in the wrong case. i.e. you might need gold_mysql.php instead of Gold_MySql.php?

吝吻 2024-10-21 16:26:27

这听起来像是服务器文件系统的区分大小写的问题。
检查路径和文件名的大小写是否正确。

linux/unix 关心大小写。
Windows 没有。


注意:这回答了问题的先前版本

It sounds like a case sensitive problem with the server filesystem.
check paths and filenames for correct case.

linux/unix take care about case.
windows does not.


note: This answers a previous version of the question

深白境迁sunset 2024-10-21 16:26:27

可能是您覆盖了包含路径。在你的index.php中尝试类似的方法

<?php

set_include_path(
    implode(PATH_SEPARATOR,
    array(
        realpath('./_controller'),
        realpath('./_resources'),
        get_include_path()
    ))
);

并删除Autoloader类中的set_include_path()调用。

恕我直言,如果您使用 Zend Framework,最好编写两个分隔符自动加载器并将它们推送到 Zend 的自动加载器堆栈。

It could be that you're overriding the include path. In your index.php try something like

<?php

set_include_path(
    implode(PATH_SEPARATOR,
    array(
        realpath('./_controller'),
        realpath('./_resources'),
        get_include_path()
    ))
);

And remove the set_include_path() calls in your Autoloader class.

IMHO, If you are using Zend Framework, it would be better to write two separator autoloaders and push them to Zend's autoloader stack.

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