Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 753 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Do most frameworks have autoloader

#1
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.

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

Is it **necessary** to have an autoloader?
Reply

#2
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.
Reply

#3
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:

[To see links please register here]

Reply

#4
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.
Reply

#5
[ZF's Autoloader][1] 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][2]. See the [Blog Post by ZF's Matthew Weier O'Phinney on Autoloading Benchmarks](

[To see links please register here]

)

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`][3].


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply

#6
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.
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through