开发者

Is autoloader different for framework vs. normal library

According to the PCR-0 proposal, all the autoloader needs is autoload() function. Zend has a few autoloading classes

Zend_Loader()
Zend_Loader_Autoloader()
Zend_Loader_Autoloader_Resource()
Zend_Loader_Autoloader_Interface()

I'm guessing it has all these classes because it's a framework, so it needs to load its own classes, as well as library classes for any libraries that the developer may add and which don't have their own autoloader.

I have a library (normal library, not a framework). Right now it doesn't have an autoloader开发者_JS百科 so I use Zend's Zend_Loader_Autoloader_Resource ->addResourceType(). If I write an autoloader for it, which autoloader will be used: Zend's or the library? and do I have to implement an autoloader as complex as Zend's or a simple one like the PCR-0 example.

What happens in these cases

  • both framework and library have their own autoloader
  • framework has autoloader, but the library doesn't
  • framework has NO autoloader, and library has


I'd suggest sticking to direct path->class mapping and implementing your own autoloader like this:

class MyLib_Autoloader
{
    public function __construct() {
        spl_autoload_register(array($this, 'autoload'));
    }
    public function autoload($className)
    {
        if (substr($className, 0, 6) == 'MyLib_') {
            include dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR strtr($className, '_', DIRECTORY_SEPARATOR) . '.php';
            return true;
        } 
        return false;
    }
}

This will check if the class is from your namespace and load the file. Assumes your autoloader is in /some/path/MyLib/ -> the include path will be /some/path/MyLib/../MyLib/ClassName.php. You can also add more logic to strip the initial MyLib from the class name and get rid of that '..'.

Make sure to use only require. It's faster and the autoload function is not called more than once for each classname! :) No need to include_once/require_once/etc. Go with the fastest.

Usage is as simple as:

include '/some/path/MyLib/Autploader.php';
$loader = new MyLib_Autoloader();


The main question is : who/how will your library be used ?

If that is by you only, then use Zend's autoloader, this will save you time. You don't need to reinvent the wheel.

If you have to make you library public and used on different projects, then that may be a problem because it will force the users of your library to have Zend Framework too. So in that case, either you make your own autoloader, either you pick one of a framework/library but include it in your library (watch out for the licenses).

Now regarding the utilisation of the autoloaders : only the autoloaders that are registered will be called. If you have your own autoloader, but you didn't mention how to setup it in your documentation, the users of your code will never think of set it up, and then it won't be used.

So the basic answer is to say : use many autoloaders, register them all, and they will be all called and everything will work fine. But that may lead to conflicts, because one autoloader will try to load something that is supposed to be handled by another autoloader, so you have to be careful with that, and not abuse of them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜