note 84086 added to function.spl-autoload-register
| From: | mailinglist dot php at hydras-world dot com | Date: | Thu, 26 Jun 2008 23:07:33 +0000 |
| Subject: | note 84086 added to function.spl-autoload-register | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-140856@lists.php.net to get a copy of this message | ||
The automatic generation of classes can be further improved to cater for files that don't
actually contain the class they were supposed to contain:
<?php
// code to set include_path...
class AutoloadException extends Exception { }
class AutoloadClass {
public static function autoload($sClassName) {
@include_once($sClassName . '.class.php');
// does the class requested actually exist now?
if (class_exists($sClassName)) {
// yes, we're done
return;
}
// no, create a new one!
eval("class $sClassName {
function __construct() {
throw new AutoloadException('Class $sClassName not found');
}
static function __callstatic(\$m, \$args) {
throw new AutoloadException('Class $sClassName not found');
}
}");
}
}
spl_autoload_register(array('AutoloadClass', 'autoload'));
?>
You might be able to expand the example above to automatically generate interfaces too...
Enjoy!
DominicC
----
Server IP: 92.48.100.16
Probable Submitter: 84.9.60.50
----
Manual Page -- http://www.php.net/manual/en/function.spl-autoload-register.php
Edit -- https://master.php.net/note/edit/84086
Del: integrated -- https://master.php.net/note/delete/84086/integrated
Del: useless -- https://master.php.net/note/delete/84086/useless
Del: bad code -- https://master.php.net/note/delete/84086/bad+code
Del: spam -- https://master.php.net/note/delete/84086/spam
Del: non-english -- https://master.php.net/note/delete/84086/non-english
Del: in docs -- https://master.php.net/note/delete/84086/in+docs
Del: other reasons-- https://master.php.net/note/delete/84086
Reject -- https://master.php.net/note/reject/84086
Search -- https://master.php.net/manage/user-notes.php