Edit report at http://bugs.php.net/bug.php?id=52412&edit=1
ID: 52412 Updated by: der...@php.net Reported by: madboyka at yahoo dot com Summary: __autoload fails to throw exception when calling a static method on the class -Status: Open +Status: Bogus Type: Bug Package: Scripting Engine problem -Operating System: Windows +Operating System: * PHP Version: 5.3.3 Block user comment: N Private report: N New Comment: Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php FWIW, this is "expected". The __autoload() method is the last line of defense for PHP to find a class definition. If it can't find it, PHP bails out with a fatal error. If you throw an exception, you basically abort this final chance, and thus gives PHP you that fatal "can not find class" error. However, you can catch the exception in the __autoload() method, Previous Comments: ------------------------------------------------------------------------ [2011-02-16 08:05:14] michael at squiloople dot com There's a slight hack of a solution tested using PHP 5.3.5 and Windows Vista: use a variable as the class name: function __autoload($class) { if (!include $class . '.php') { throw new Exception('Cannot autoload ' . $class); } } $class = 'Application'; try { $class::start(); } catch (Exception $e) { echo $e->getMessage(); } // Outputs the exception as expected. ------------------------------------------------------------------------ [2010-09-25 23:39:25] alex dot offshore at gmail dot com Temporary solution. Caveats and notices: - The class actually WILL BE DEFINED ANYWAY; - 'eval' usage; - __callStatic used to avoid "method not found" error. <?php function __autoload($className) { echo "Want to load $className.\n"; // assuming we can not load class // error handling code { eval('class ' . $className . ' { static function __callStatic($n, $a) { return false; } }'); throw new Exception("Unable to load $className."); } } try { //new MissingClass(); // works as expected MissingClass::someFunction(); } catch (Exception $e) { echo 'CAUGHT: ' . $e->getMessage(), "\n"; } ------------------------------------------------------------------------ [2010-09-03 03:26:56] php dot net at phrozenbyte dot de Same on Ubuntu 10.04 / Apache 2.2 and CLI mode Test script: --------------- <?php spl_autoload_register( function($autoload) { throw new Exception(); } ); try { Foo::bar(); } catch(Exception $e) { echo "Exception caught\n"; } ?> Expected result: ---------------- Exception caught Actual result: -------------- Fatal error: Class 'Foo' not found in /home/daniel/www/other/php-bug.php on line 0 ------------------------------------------------------------------------ [2010-07-23 09:34:02] madboyka at yahoo dot com Description: ------------ I've tried to do the following: 1. Wrote and autoload method, that throws an exception. 3. Made a static call on a non-existing class within a try block. Tried this on windows 7 / Apache 2.2 / PHP 5.3.3. Test script: --------------- <?php function __autoload($class_name) { throw new Exception($class_name); } try { Application::start(); // new Application(); works fine } catch (Exception $ex) { var_dump($ex); } Expected result: ---------------- The script should var_dump() an exception with the Message 'Application' as it does when instantiating a class. Actual result: -------------- The script dies with Fatal error: Class 'Application' not found. ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=52412&edit=1