Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Standard PHP Library (SPL) Functions : spl_autoload_register

spl_autoload_register

Register given function as __autoload() implementation (PHP 5 >= 5.1.2)
bool spl_autoload_register ( [callback autoload_function] )


Code Examples / Notes » spl_autoload_register

harvey dot no_spam dot robin

This function is smart enough not to add the same loader twice.  This seems to work for all of the different loader formats.  Example:
<?php
class ALoader
{
 static function load($class) { return true; }
}
function anotherLoader($class) {
 return true;
}
$F = new ALoader;
spl_autoload_register(array('ALoader', 'load'));
spl_autoload_register(array('ALoader', 'load'));
spl_autoload_register(array($F, 'load'));
spl_autoload_register('anotherLoader');
spl_autoload_register('anotherLoader');
var_dump(spl_autoload_functions());
/*
* Results on PHP5.2 CLI, linux.
* array(2) {
*  [0]=>
*  array(2) {
*    [0]=>
*    string(7) "ALoader"
*    [1]=>
*    string(4) "load"
*  }
*  [1]=>
*  string(13) "anotherLoader"
* }
*/
?>


stanlemon

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.


florent

If your autoload function is a class method, you can call spl_autoload_register with an array specifying the class and the method to run.
* You can use a static method :
<?php
class MyClass {
 public static function autoload($className) {
   // ...
 }
}
spl_autoload_register(array('MyClass', 'autoload'));
?>
* Or you can use an instance :
<?php
class MyClass {
 public function autoload($className) {
   // ...
 }
}
$instance = new MyClass();
spl_autoload_register(array($instance, 'autoload'));
?>


Change Language


Follow Navioo On Twitter
ArrayIterator::current
ArrayIterator::key
ArrayIterator::next
ArrayIterator::rewind
ArrayIterator::seek
ArrayIterator::valid
ArrayObject::append
ArrayObject::__construct
ArrayObject::count
ArrayObject::getIterator
ArrayObject::offsetExists
ArrayObject::offsetGet
ArrayObject::offsetSet
ArrayObject::offsetUnset
CachingIterator::hasNext
CachingIterator::next
CachingIterator::rewind
CachingIterator::__toString
CachingIterator::valid
CachingRecursiveIterator::getChildren
CachingRecursiveIterator::hasChildren
DirectoryIterator::__construct
DirectoryIterator::current
DirectoryIterator::getATime
DirectoryIterator::getCTime
DirectoryIterator::getFilename
DirectoryIterator::getGroup
DirectoryIterator::getInode
DirectoryIterator::getMTime
DirectoryIterator::getOwner
DirectoryIterator::getPath
DirectoryIterator::getPathname
DirectoryIterator::getPerms
DirectoryIterator::getSize
DirectoryIterator::getType
DirectoryIterator::isDir
DirectoryIterator::isDot
DirectoryIterator::isExecutable
DirectoryIterator::isFile
DirectoryIterator::isLink
DirectoryIterator::isReadable
DirectoryIterator::isWritable
DirectoryIterator::key
DirectoryIterator::next
DirectoryIterator::rewind
DirectoryIterator::valid
FilterIterator::current
FilterIterator::getInnerIterator
FilterIterator::key
FilterIterator::next
FilterIterator::rewind
FilterIterator::valid
LimitIterator::getPosition
LimitIterator::next
LimitIterator::rewind
LimitIterator::seek
LimitIterator::valid
ParentIterator::getChildren
ParentIterator::hasChildren
ParentIterator::next
ParentIterator::rewind
RecursiveDirectoryIterator::getChildren
RecursiveDirectoryIterator::hasChildren
RecursiveDirectoryIterator::key
RecursiveDirectoryIterator::next
RecursiveDirectoryIterator::rewind
RecursiveIteratorIterator::current
RecursiveIteratorIterator::getDepth
RecursiveIteratorIterator::getSubIterator
RecursiveIteratorIterator::key
RecursiveIteratorIterator::next
RecursiveIteratorIterator::rewind
RecursiveIteratorIterator::valid
SimpleXMLIterator::current
SimpleXMLIterator::getChildren
SimpleXMLIterator::hasChildren
SimpleXMLIterator::key
SimpleXMLIterator::next
SimpleXMLIterator::rewind
SimpleXMLIterator::valid
class_implements
class_parents
iterator_count
iterator_to_array
spl_autoload_call
spl_autoload_extensions
spl_autoload_functions
spl_autoload_register
spl_autoload_unregister
spl_autoload
spl_classes
spl_object_hash
eXTReMe Tracker