0

我正在尝试将SwiftMailer php 库与我编写的程序一起使用。在包含这个库之前,我一直在使用 spl_autoload_register() 函数。然而,在使用这个库之前,我使用 spl 函数明确定义了类扩展和位置:

set_include_path(get_include_path().[my include path]);
spl_autoload_extensions('.class.php');
spl_autoload_register();
session_start();

我遇到的问题是,现在我正在尝试使用一个不遵循相同命名约定的库。他们自己的自动加载类(由对库的初始调用构建)看起来像这样。

public static function autoload($class)
{
//Don't interfere with other autoloaders
if (0 !== strpos($class, 'Swift_'))
{
  return;
}

$path = dirname(__FILE__).'/'.str_replace('_', '/', $class).'.php';

if (!file_exists($path))
{
  return;
}

if (self::$initPath && !self::$initialized)
{
  self::$initialized = true;
  require self::$initPath;
}

require $path;

}

当我在调用他们的课程后尝试简单地运行程序时,我得到:

Fatal error: spl_autoload() [<a href='function.spl-autoload'>
function.spl-autoload</a>]:Class Swift_MailTransport could not
be loaded in [my file] on line 30 

第 30 行:

 $transport = Swift_MailTransport::newInstance();

我曾尝试使用仿照他们的自定义自动加载类,但是,当我尝试时,我得到的只是:

var_dump(spl_autoload_functions());

结果:

bool(false);

我知道这必须是一个相当简单的问题,我忽略了一些东西,但我找不到它。

任何帮助将不胜感激。

4

2 回答 2

0

尝试删除这个:

spl_autoload_register();

文档中:

[if] spl_autoload_register() is called without any parameters then 
  [spl_autoload(...)] functions will be used

知道了这一点,认为 spl_autoload 不知道在哪里加载 SwiftMailer 类是合乎逻辑的,因为你得到的错误是这样说的。然后,SwiftMailer 不在您的包含路径中,因为 spl_autoload 尝试从那里加载。

下一步是将您的 SwiftMailer 类放在一个包含路径中。

于 2011-11-07T16:17:25.300 回答
0

好吧,在把我的头撞到墙上,一无所获之后,我从同样是程序员的兄弟那里得到了很好的反馈。

整个问题,源于这一行:

require_once(SITE_ROOT.'/classes/lib/swift_required.php');

SITE_ROOT 变量实际上是在引用 Web 位置(即 http://),对于我当前的主机,这不起作用,它需要使用物理文件位置。进行此更改后,随附的自动加载器将按照宣传的方式工作。

于 2011-11-07T23:03:59.823 回答