nicorellius
nicorellius

Reputation: 4053

Odd PHP Class autoloader Behavior - PEAR Related

I have a strange problem I can't figure out. I'm using PEAR to send mail from a PHP page. The Send_Mail class is working (sending mail using SMTP), but I'm getting this strange warning associated with my 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`

In my initialize.php file, I have this:

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

spl_autoload_register('autoloader');

And in my header.php file I am loading several PHP classes for the site:

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

The error referes to a LOGIN class which I don't have. But I searched my entire site folder, including php and found these two lines in 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);

When I comment out the line containing LOGIN I get the same warning but for PLAIN and when I comment out both lines, the warnings go away (but then SMTP authentication fails).

Why is this happening?

UPDATE

Here is my new autoloader:

function autoloader($class) {

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

When I echo 'classes' . $class . '.php', though, I get this:

classes/.php

And then if I change it back to not use the file_exists it works, but the echo still shows classes/.php

Upvotes: 0

Views: 127

Answers (2)

nicorellius
nicorellius

Reputation: 4053

I reverted back to a simple autoloader and things seem to be working fine:

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

spl_autoload_register('autoloader');

Thanks for suggestions.

Upvotes: 0

Jonathan Rich
Jonathan Rich

Reputation: 1738

I'm not sure what version of Net_SMTP you're on, but the setAuthMethod function takes as a parameter various different types of structures - method names, classes, objects, callables, etc.

Since PHP is dynamically typed, SMTP.php in Net_SMTP has to do a lot of checking on that object in order to determine what type of object it is. As part of that, it tries to determine if 'LOGIN' is a class name, which is invoking your autoloader.

The solution is to perform a file_exists in your autoloader before trying the include, but of course if your include path is complex in any way you're basically setting yourself up for headaches. Anyone who has dealt with writing a comprehensive autoloader knows your pain.

Upvotes: 1

Related Questions