奇怪的PHP类自动加载器行为 - PEAR相关

时间:2013-02-15 18:06:45

标签: php pear autoloader

我有一个奇怪的问题,我无法弄清楚。我正在使用PEAR从PHP页面发送邮件。 Send_Mail类正在运行(使用SMTP发送邮件),但是我收到了与autoloader相关的奇怪警告。

Warning: include(classes/LOGIN.php) 
[<a href='function.include'>function.include</a>]: 
failed to open stream: No such file or directory in 
C:\xampp\htdocs\mysite\initialize.php on line 46`

在我的initialize.php文件中,我有这个:

function autoloader($class) {
    include 'classes/' . $class . '.php';
}

spl_autoload_register('autoloader');

在我的header.php文件中,我正在为该站点加载几个PHP类:

// autoload PHP classes for site
autoloader('Navigation');
autoloader('Validation');

错误引用了我没有的LOGIN类。但我搜索了整个网站文件夹,包括php,并在C:\xampp\php\PEAR\Net\SMTP.php中找到了这两行:

/* These standard authentication methods are always available. */
$this->setAuthMethod('LOGIN', array($this, '_authLogin'), false);
$this->setAuthMethod('PLAIN', array($this, '_authPlain'), false);

当我注释掉包含LOGIN的行时,我会收到相同的警告,但对于PLAIN,当我注释掉这两行时,警告会消失(但后来SMTP验证失败)。

为什么会这样?

更新

这是我的新autoloader

function autoloader($class) {

    if (file_exists('classes' . $class . '.php')) {
        include 'classes' . $class . '.php'; 
    }
}

当我回复'classes' . $class . '.php'时,我得到了这个:

classes/.php

然后,如果我将其更改为不使用file_exists它可以正常工作,但echo仍会显示classes/.php

2 个答案:

答案 0 :(得分:1)

我不确定您使用的是哪个版本的Net_SMTP,但setAuthMethod函数将各种不同类型的结构 - 方法名称,类,对象,可调用对象等作为参数。

由于PHP是动态类型的,因此Net_SMTP中的SMTP.php必须对该对象进行大量检查才能确定它是什么类型的对象。作为其中的一部分,它试图确定'LOGIN'是否是一个类名,它正在调用您的自动加载器。

解决方法是在尝试包含之前在自动装带器中执行file_exists,但当然如果您的包含路径在任何方面都很复杂,那么您基本上会让自己陷入困境。任何处理过编写全面自动装载机的人都知道你的痛苦。

答案 1 :(得分:0)

我又回到了一个简单的autoloader,似乎工作正常:

function autoloader($class) {
    include 'classes/' . $class . '.php';
}

spl_autoload_register('autoloader');

感谢您的建议。

相关问题