开发者

Do most frameworks have autoloader

开发者 https://www.devze.com 2023-03-11 14:03 出处:网络
The framework that I use (Zend) has an autoloader built it. I think this autoloader takes care of loading, not only Zend itself, but also any other libraries the developer may add to the project.

The framework that I use (Zend) has an autoloader built it. I think this autoloader takes care of loading, not only Zend itself, but also any other libraries the developer may add to the project.

开发者_如何学Go

Is it normal practice for a framework to have an autoloader? Any that don't have?

Is it necessary to have an autoloader?


ZF's Autoloader will by default only load ZF's classes. It can (and is supposed to be) configured to also load your custom classes and you can also add additional Autoloader from other frameworks. ZF 2 will have a classmap autoloader in addition to the PSR-0 autoloader. See the Blog Post by ZF's Matthew Weier O'Phinney on Autoloading Benchmarks

Yes, it's common to have autoloaders because you dont want require statements all over your code. It's not strictly necessary though. The native way to achive autoloading is with spl_autoload_register.


Not it is not necessary, but it is very convenient. You don't need a hell of a lot of includes or requires, and units are only loaded when you need the class it contains. And that helps you stucture your code too, because incorrect naming will cause the autoloader to fail.


As stated by other members that not every framework has an Autoloader but I believe that every framework should.

If you can understand what a framework does than you should understand that there designed to be modular, being able to add and remove components easily, if you was to hard code all your libraries models, controllers etc then when you add new libraries you would have to recode for the library to work.

So that's the convenience part of the autoloader, the other part is the performance and resource management side of it, The base idea of the autoloader is to load the files only when required.

so on your index.php page you may need 5 libraries, but on your register.php you may only need 2 libraries, so that using an autoloader will save you 3 includes on your register page.

It also allows a better file structure as your components are separated and organized.

as you may be looking into placing an autoloader into your framework i strongly recommend you look at the PSR-0 standards that is currently in use in Zend and many other high end framework, the link can be found below:

http://groups.google.com/group/php-standards/web/psr-0-final-proposal


Yes, it is a common practice to include autoloader in the framework.

No, I can not think of any major framework without autoloader.

Yes, it is necessary if you want the framework to be flexible enough. It eases the addition of modules / classes to simply copying the files to the correct directory and invoking them from within the code. Autoloader just includes proper files instead of including all of the files (even if they are not needed at the moment).

Additionally, the way the classes are autoloaded can be determined completely by the autoloader mechanism, enforcing eg. proper directory structure or enforcing prior declaration of classes / files that can be autoloaded.


Many frameworks do have autoloaders, one that does not is CodeIgniter and you have to specify which classes you want to load all the time and which classes you want to load inside your controllers / models / views / libraries.

It isn't completely necessary, but it is nice because it generally enforces a file structure and you also don't have to worry about remembering to include files at the top of your scripts.

[Edit]

Apparently clarification is needed on my CodeIgniter statement. Yes there is an auto_load place that lets you specify the libraries/modules that you want to load automatically at the start of every script, and yes there is a loader library, but they are not the same as a real autoloader.

You have to specify which ones you want at the start, it doesn't know that you want to load the Pizza_Store modle until you use the custom

$this->load->model->('foo');

which is equivalent to

include 'application/models/foo.php';
$this->Foo = new foo();

But is not really the same as only having to call $this->foo = new Models_Foo(); and CodeIgniter knowing what you mean by it, which is what happens when an autoloader is used.

[Edit part deux]

Nowehere in the code does it say __autoload, spl_autoload or spl_autoload_register. This is because it maintains PHP 4 compatibility. It is just the way that the framework has been built, there is a false autoload as I stated above, but it does this for each "autoloaded" class:

foreach($autoloaded_class as $type=>$class){
    if(is_application_class($class)){
        include "application/{$type}/{$class}.php";
    }
    elseif(is_core($class)){
        include "core/{$type}/{$class}.php";
    }
}
$array['Foo'] = new Foo();
$controller($array);

Which is essentially calling:

include 'foo.php';

at the top of every file no matter if you need it or not.

0

精彩评论

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