Page 1 of 1

__autoload() problem?

Posted: Fri Sep 24, 2010 12:54 pm
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?

Re: __autoload() problem?

Posted: Fri Sep 24, 2010 12:59 pm
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?

Re: __autoload() problem?

Posted: Fri Sep 24, 2010 1:11 pm
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.

Re: __autoload() problem?

Posted: Fri Sep 24, 2010 1:15 pm
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.

Re: __autoload() problem?

Posted: Fri Sep 24, 2010 1:36 pm
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).

Re: __autoload() problem?

Posted: Fri Sep 24, 2010 2:42 pm
by MichaelR
Looks like I should have checked PHP bugs. It's been reported as one here.