__autoload() problem?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MichaelR
Forum Contributor
Posts: 148
Joined: Sat Jan 03, 2009 3:27 pm

__autoload() problem?

Post by MichaelR »

I've having a little trouble with the __autoload() function. It's best explained with an example:

Expected (/application/models/MySQLiConnection.php does NOT exist):

Code: Select all


  function __autoload($class) {

    if (!include('/application/models/' . $class . '.php')) {
      throw new Exception('Unable to call ' . $class);
    }

  }

  try {
    $one = new MySQLiConnection();
  }

  catch (Exception $e) {
    echo $e->getMessage();
  }

  // Outputs:  "Unable to call MySQLiConnection."
Unexpected (/application/models/MySQLiConnection.php does NOT exist):

Code: Select all


  function __autoload($class) {

    if (!include('/application/models/' . $class . '.php')) {
      throw new Exception('Unable to call ' . $class);
    }

  }

  try {
    $one = MySQLiConnection::SetConnection();
  }

  catch (Exception $e) {
    echo $e->getMessage();
  }

  // Outputs: "Fatal error: Class 'MySQLiConnection' not found in C:\Users\Michael\Websites\index.php on line 12."
As a comparison, if the file DOES exist then __autoload() works fine both when calling a class using the constructor and when calling using a static method. Is this a bug, or is this explicable behaviour?
Last edited by MichaelR on Fri Sep 24, 2010 2:08 pm, edited 4 times in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: __autoload() problem?

Post by John Cartwright »

Code: Select all

include('/application/models/MySQLiConnection.php');
var_dump(class_exists('MySQLiConnection'));
Disabling the autoload for a second, what does this return?

Also, your examples paths show MySQLiConnection, but your using MySQLConnection. Typo?
MichaelR
Forum Contributor
Posts: 148
Joined: Sat Jan 03, 2009 3:27 pm

Re: __autoload() problem?

Post by MichaelR »

Also, your examples paths show MySQLiConnection, but your using MySQLConnection. Typo?
Yes, typo in my explanation here; not in the actual code I'm using.
Disabling the autoload for a second, what does this return?
bool(false)

It seems to work fine when calling a non-existing class file using the constructor. It's only when I change to a static method that the try { } catch { } block seems to stop working.

EDIT: Sorry, I completely messed up the thread when I posted it and forgot to change the second code example. Should be clear now.
Last edited by MichaelR on Fri Sep 24, 2010 2:08 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: __autoload() problem?

Post by John Cartwright »

The Manual wrote:Prior to 5.3.0, exceptions thrown in the __autoload function could not be caught in the catch block and would result in a fatal error. From 5.3.0+ exceptions thrown in the __autoload function can be caught in the catch block, with 1 provision. If throwing a custom exception, then the custom exception class must be available. The __autoload function may be used recursively to autoload the custom exception class.
MichaelR
Forum Contributor
Posts: 148
Joined: Sat Jan 03, 2009 3:27 pm

Re: __autoload() problem?

Post by MichaelR »

I'm using PHP 5.3.3 and I'm not using a custom exception class. As far as I can see, it's a bug. It correctly throws the exception and catches it if I try to call a non-existent file class using the constructor method but not if I use a static method. This is just bizarre (especially as the autoloader has no issue in loading an existing file class if called using a static method).
MichaelR
Forum Contributor
Posts: 148
Joined: Sat Jan 03, 2009 3:27 pm

Re: __autoload() problem?

Post by MichaelR »

Looks like I should have checked PHP bugs. It's been reported as one here.
Post Reply