#29648 [Opn]: cant throw exceptions with __autoload (to catch and handle them - workaround?)

From: Date: Fri, 13 Aug 2004 10:49:40 +0000
Subject: #29648 [Opn]: cant throw exceptions with __autoload (to catch and handle them - workaround?)
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-64539@lists.php.net to get a copy of this message
ID: 29648 User updated by: black at scene-si dot org Reported By: black at scene-si dot org Status: Open Bug Type: Scripting Engine problem Operating System: debian PHP Version: 5.0.0 New Comment: And the result: Fatal error: __autoload(object_wtf) threw an exception of type 'Exception' in /root/monotek/mono/commands/test.php on line 16 (gah) Previous Comments: ------------------------------------------------------------------------ [2004-08-13 12:47:52] black at scene-si dot org ugh, forgot to add if (!class_exists($classname)) { throw new Exception(); } after the first include_once in the example. Ofcourse i'm throwing the exception im trying to catch ;) ------------------------------------------------------------------------ [2004-08-13 12:30:17] black at scene-si dot org Description: ------------ I've tried to create an __autoload() function which would throw an exception if it can't load the class name from a file depending on the classname i request with the php code. I couldnt catch the exception with a try/catch clause, because it always failed (see expected/actual result below). My workaround was to add this after the first include_once statement inside the __autoload: if (!class_exists($classname)) { eval("class ".$classname." { }"); } Afterwards instead of a try/catch clause i used a method_exist call to see if a general function was defined (one that i require to have). Reproduce code: --------------- <?php function __autoload($classname) { if (substr($classname,0,7)=="object_") { @include_once("include/object.".substr($classname,7).".php"); return; } @include_once("class.".$classname.".php"); } try { $object = new object_wtf("hello"); } catch (Exception $e) { echo "__autoload failed "; var_dump($object); } Expected result: ---------------- Output: __autoload failed NULL Actual result: -------------- Fatal error: Class 'object_wtf' not found in /root/monotek/mono/commands/test.php on line 15 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=29648&edit=1

« previous php.bugs (#64539) next »