简体   繁体   English

spl_autoload_register() 在不同目录中

[英]spl_autoload_register() in different directories

I have a file that auto loads each of my classes.我有一个文件可以自动加载我的每个类。 This is what it contains:这是它包含的内容:

spl_autoload_register(function($class){
    require_once 'classes/' . $class . '.php';
});
require_once 'functions/sanitize.php';
require_once 'functions/hash.php';

But when I require_once this file from another php file that is inside my ajax folder, it will try looking for the classes , the function will look from my classes with the path: main_folder/ajax/classes instead of just main_folder/classes .但是,当我从ajax文件夹内的另一个 php 文件中require_once这个文件时,它会尝试查找,该函数将从我的类中查找路径: main_folder/ajax/classes而不是main_folder/classes

Does anyone know how to fix this ?有谁知道如何解决这一问题 ?

FIX :修复

spl_autoload_register(function($class){
    if (file_exists('classes/' . $class . '.php')) {
       require_once 'classes/' . $class . '.php';
    }
    elseif (file_exists('../classes/' . $class . '.php')) {
       require_once '../classes/' . $class . '.php';
    }
    elseif (file_exists('../../classes/' . $class . '.php')) {
       require_once '../../classes/' . $class . '.php';
    }

You should simple use this function just once - in the main file (usually index.php) and not in another files.你应该简单地使用这个函数一次 - 在主文件(通常是 index.php)中而不是在另一个文件中。

However if it's not possible (but I don't see any reason when could it be not possible) you can change it for example that way:但是,如果这是不可能的(但我看不出任何理由何时不可能),您可以通过以下方式更改它:

spl_autoload_register(function($class){
    if (file_exists('classes/' . $class . '.php')) {
       require_once 'classes/' . $class . '.php';
    }
    elseif (file_exists( $class . '.php')) {
       require_once $class . '.php';
    }
});

You need to know the absolute path to the classes directory, then just use a full qualified path to get there.您需要知道类目录的绝对路径,然后只需使用完整的限定路径即可到达。

Make sure your document root is correctly configured with your http server.确保您的文档根目录与您的 http 服务器正确配置。 Furthermore this is usually solved by routing all requests to index.php and deriving the base path (document root) from there.此外,这通常通过将所有请求路由到 index.php 并从那里派生基本路径(文档根)来解决。 Here is your code modified:这是您修改的代码:

spl_autoload_register(function($class) {
    $resolved = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . $class . '.php';
    if(file_exists($resolved)) {
      require_once $resolved;
    } else {
      // make it known to your that a class failed to load somehow
      return;
    }
})

Here is another implementation that I use for simple projects这是我用于简单项目的另一个实现

Here is a proper way, It should universally work.这是一个正确的方法,它应该普遍有效。

spl_autoload_register(function ($classname) {
    $classpath = sprintf('%s' . DIRECTORY_SEPARATOR . '%s.php', dirname(realpath(__FILE__)), basename($classname, '.{php,PHP}'));

    if (file_exists($classpath)) {
        echo "Loading <b>$classpath</b>...<br>";
        require($classpath);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM