|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2004-08-13 12:30 UTC] 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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 01 05:00:01 2025 UTC |
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 ;)