Hi, I am currently trying to integrate a plugin to an existing application. My own plugin consist of an autoloading class, however, the application itself also already consist of an autoloading class. How do I go about adding the 2nd one?
The existing application autoload class:
new AutoLoad( );
class AutoLoad {
public function __construct( ) {
spl_autoload_register( array( $this, 'load' ) );
}
public function load( $className ) {
}
My own autoload function.
function __autoload($class_name)
{
Should I change my autoload function to a class and extend the already existing AutoLoad class? Does it work this way?
You should use the [fphp]spl_autoload_register[/fphp] function as their class already does. You don’t necessarily need to make it a class, but you do need to use [fphp]spl_autoload_register[/fphp].
You can also use a closure in php 5.3 for this as follows:
spl_autoload_register( function( $path ) {
// body of your autoload function.
});
Off Topic:
highlight tag seems broken.
Thanks Michael. I tried doing it this way:
require_once APP_ROOT.'/includes/autoloader.inc.php';
spl_autoload_register( '__autoload' );
But I got exception:
Fatal error: Uncaught exception ‘LogicException’ with message ‘Function __autoload’ not found (function ‘__autoload’ founf or invalid function name)
Any idea what went wrong?
Hi Michael, I got the logicexception sorted out. Its because I forgot adding in the namespace. Thanks alot.
No!! You CANNOT use the magic function name __autoload and the spl_autoload_register system and expect them both to work. You must either change the name of your function to something non-magical, or pass it as an anonymous function (that is a closure) as I described.