note 78122 added to function.spl-autoload-register
| From: | stanlemon at mac dot com | Date: | Fri, 28 Sep 2007 17:20:04 +0000 |
| Subject: | note 78122 added to function.spl-autoload-register | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-130885@lists.php.net to get a copy of this message | ||
The spl_autoload_register() method registers functions in its stack in the order that
spl_autoload_register() was called, and subsequently if you want an autoload function to override
previous autoload functions you will either need to unregister the previous ones or change the order
of the autoload stack.
For example, say in your default implementation of an autoload function you throw an exception if
the class cannot be found, or perhaps a fatal error. Later on in your code you add a second
implementation of an autoload function which will load a library that the previous method would fail
on. This will not call the second autoloader method first, but rather will continue to error out on
the first method.
As previously mentioned, you can unregister the existing autoloader that errors out, or you can
create a mechanism for unregistering and re-registering the autoloaders in the order you want.
Here is a sample/example of how you might consider re-registering autoloaders so that the newest
autoloader is called first, and the oldest last:
<?php
function spl_autoload_preregister( $autoload ) {
// No functions currently in the stack.
if ( ($funcs = spl_autoload_functions()) === false ) {
spl_autoload_register($func);
} else {
// Unregister existing autoloaders...
foreach ($funcs as $func) {
spl_autoload_unregister($func);
}
// Register the new one, thus putting it at the front of the stack...
spl_autoload_register($autoload);
// Now, go back and re-register all of our old ones.
foreach ($funcs as $func) {
spl_autoload_register($func);
}
}
}
?>
Note: I have not tested this for overhead, so I am not 100% sure what the performance implication of
the above example are.
----
Server IP: 69.147.83.197
Probable Submitter: 72.22.26.23
----
Manual Page -- http://www.php.net/manual/en/function.spl-autoload-register.php
Edit -- https://master.php.net/note/edit/78122
Del: integrated -- https://master.php.net/note/delete/78122/integrated
Del: useless -- https://master.php.net/note/delete/78122/useless
Del: bad code -- https://master.php.net/note/delete/78122/bad+code
Del: spam -- https://master.php.net/note/delete/78122/spam
Del: non-english -- https://master.php.net/note/delete/78122/non-english
Del: in docs -- https://master.php.net/note/delete/78122/in+docs
Del: other reasons-- https://master.php.net/note/delete/78122
Reject -- https://master.php.net/note/reject/78122
Search -- https://master.php.net/manage/user-notes.php