Swift Mailer 破坏了自动装载

Swift Mailer ruins autolading

我正在编写使用 Swift Mailer library 的简单 PHP 应用程序。我的应用既不使用命名空间也不使用 composer。

但是,在要求 swift_required.php 我的(模型)classes 之后没有找到(致命错误:Class 'Format' 没有找到 被 PHP 解释抛出。

自动装载

define("_DOCUMENT_ROOT", str_replace("//", "/", $_SERVER['DOCUMENT_ROOT'] . "/"));
    function __autoload($class_name) {
        $file_name = $class_name . '.php';
        $include_foleder = array("php/model/", "templates/","cron/crons_tasks/");
        foreach ($include_foleder as $folder) {
            $abs_path = _DOCUMENT_ROOT . $folder . $file_name;
            if (file_exists($abs_path)) {
                require_once $abs_path;
            }
        }
    }

函数有问题的部分

  $bar = Format::bar($foo); //works fine
  require_once _DOCUMENT_ROOT . "php/lib/swiftmailer-master/lib/swift_required.php"; //works fine
  $bar = Format::bar($foo); //Class not found

Class Format 是我的习惯 class,位于 _DOCUMENT_ROOT . php/model/Format.php。其他自定义 classes(来自模型文件夹)在需要 Swift 后找不到 Mailer。

所以我猜测我以前的自动加载以某种方式被 Swift Mailer 覆盖,这可能吗?

谢谢。

而不是 __autoload(),您应该使用 spl_autoload_register。

If there must be multiple autoload functions, spl_autoload_register() allows for this. It effectively creates a queue of autoload functions, and runs through each of them in the order they are defined. By contrast, __autoload() may only be defined once.

http://php.net/manual/en/function.spl-autoload-register.php

define("_DOCUMENT_ROOT", str_replace("//", "/", $_SERVER['DOCUMENT_ROOT'] . "/"));

spl_autoload_register(function($class_name) {
    $file_name = $class_name . '.php';
    $include_folder = array("php/model/", "templates/","cron/crons_tasks/");
    foreach ($include_folder as $folder) {
        $abs_path = _DOCUMENT_ROOT . $folder . $file_name;
        if (file_exists($abs_path)) {
            require_once $abs_path;
        }
    }
});