1

我正在使用以下代码段从多个文件夹中自动加载类:

// Check if the autoload configuration file exists
if(is_file("configuration/autoload")) {
    // Extract the listed folders from the configuration file
    $folders = explode("\n", file_get_contents("configuration/autoload"));
    // Prepend the base path to the extracted folder paths
    array_unshift($folders, get_include_path());
    // Configure the folders in which to attempt class autoloading
    set_include_path(implode(PATH_SEPARATOR, $folders));
    // Configure the file extensions that should be autoloaded
    spl_autoload_extensions(".php");
    // Administer the attempt to autoload classes
    spl_autoload_register();
}

几个文件夹列在一个文件中,如下所示:

core/utility
core/factory
core/modules
core/classes
core/classes/form
core/classes/form/fields
frontend

它在本地像魅力一样工作,但我无法让它在我的在线服务器上工作(我做了 CHMOD 所有涉及的文件和文件夹)。我想在设置包含路径时出了点问题,但我似乎无法理解它。

有任何想法吗?

谢谢

4

2 回答 2

1

我建议创建您自己的自动加载功能,即 my_autoloader。这样您就可以完全控制文件夹处理。

function my_autoloader($className)
{
    $parts = explode('\\', $className); //split out namespaces
    $classname = strtolower(end($parts)); //get classname case insensitive (just my choice)

        //TODO: Your Folder handling which returns classfile

    require_once($loadFile); 
}
spl_autoload_register(__NAMESPACE__ . '\my_autoloader');

记住要处理不同的命名空间

于 2013-01-08T23:05:40.030 回答
0

这就是 Magento 电子商务的做法:

function __autoload($class)
{
    if (defined('COMPILER_INCLUDE_PATH')) {
        $classFile = $class.'.php';
    } else {
        $classFile = uc_words($class, DIRECTORY_SEPARATOR).'.php';
    }

    include($classFile);
}

那么你将有以下结构:

class Company_Category_Class{}

以及以下限制(假设您的包含路径中有“lib”):

./lib/Company/Category/Class.php

如果您有任何问题,请告诉我。

于 2013-01-08T23:14:54.670 回答