2013-01-08 55 views
1

我使用下面的代码片段自动加载的类从几个文件夹:spl_autoload_register()与多个文件夹

// 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所涉及的所有文件&文件夹)。我想在设置包含路径的时候,事情出错了,但我似乎无法围绕它来包裹我的头。

任何想法?

感谢

+0

大多数情况下,当我听到服务器上的自动加载中断时,这是因为服务器通常在运行Linux并且区分大小写的文件系统。 OS X和Windows在默认情况下不区分大小写。 所以问题可能只是你试图自动加载一个不同于文件名的外壳的类。 – Evert

+0

@Evert我为这两个类和它们的文件名应用相同的格式设置 –

回答

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'); 

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

+0

实际上归结为:我设置我的包含路径是动态的,因为我**不想**具有“Zend-like”类名比如Framework_Controller_SomeClass(就像Magento太平常一样)。也许这个问题太多了,但是因为它像本地的魅力一样工作 - 我认为我距离实现目标只有一小步 –

+0

我的代码中没有使用类似Zend的类名。我只是使用'if(file_exists($ current_folder。“\”。$ classname。“.php”)'...'来遍历这些文件夹......并且如果您确定您从未在代码中使用名称空间,那么您也可以忽略处理PHP中的命名空间结构 –

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 

让我知道如果您有任何问题。

+0

事情是,我试图通过不必将特定的层次结构应用于我的类名,我知道它可以完成,因为它在本地工作,所以我不能太远! –

+0

所以只需稍加修改即可./lib/Company_Category_Class.php? – Nitroware