大多数框架都有自动加载器吗
我使用的框架(Zend)有一个内置的自动加载器。我认为这个自动加载器不仅负责加载 Zend 本身,还负责加载开发人员可能添加到项目中的任何其他库。
框架拥有自动加载器是正常做法吗?有没有的吗?
是否有必要拥有自动加载器?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
ZF 的 Autoloader 默认情况下仅加载 ZF 的类。它可以(并且应该)配置为加载您的自定义类,您还可以从其他框架添加其他自动加载器。除了 PSR 之外,ZF 2 还将拥有一个类映射自动加载器-0自动加载器。请参阅 ZF 的 Matthew Weier O'Phinney 关于自动加载基准的博客文章
是的,自动加载器很常见,因为您不希望代码中到处都是 require 语句。但这并不是绝对必要的。实现自动加载的本机方法是使用
spl_autoload_register
。
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
.不是没有必要,而是非常方便。您不需要大量的
includes
或requires
,并且仅当您需要单元包含的类时才会加载单元。这也可以帮助您构建代码,因为不正确的命名会导致自动加载器失败。Not it is not necessary, but it is very convenient. You don't need a hell of a lot of
includes
orrequires
, 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.正如其他成员所说,并非每个框架都有自动加载器,但我相信每个框架都应该有。
如果您能理解框架的作用,那么您应该理解框架的设计是模块化的,能够轻松添加和删除组件,如果您要对所有库模型、控制器等进行硬编码,那么当您添加新库时,您将拥有重新编码以使库能够工作。
这就是自动加载器的便利部分,另一部分是它的性能和资源管理方面,自动加载器的基本思想是仅在需要时加载文件。
因此,在您的index.php页面上您可能需要5个库,但在您的register.php上您可能只需要2个库,因此使用自动加载器将在您的注册页面上节省3个包含。
当您的组件被分离和组织时,它还允许更好的文件结构。
由于您可能正在考虑将自动加载器放入您的框架中,我强烈建议您查看 Zend 和许多其他高端框架中当前使用的 PSR-0 标准,可以在下面找到链接:
http://groups.google.com/group/ php-standards/web/psr-0-final-proposal
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.
许多框架确实有自动加载器,其中一个没有的是 CodeIgniter,您必须指定要始终加载哪些类以及要在控制器/模型/视图/库中加载哪些类。
它不是完全必要的,但它很好,因为它通常强制执行文件结构,并且您也不必担心记住在脚本顶部包含文件。
[编辑]
显然我的
CodeIgniter
声明需要澄清。是的,有一个 auto_load 位置,可以让您指定要在每个脚本开始时自动加载的库/模块,是的,有一个加载器库,但它们与真正的自动装弹机。你必须在开始时指定你想要哪些,它不知道你想要加载 Pizza_Store 模型,直到你使用
相当于
But 的自定义,它与只需要调用
$this 并不相同->foo = new Models_Foo();
并且 CodeIgniter 知道你的意思,这就是使用自动加载器时发生的情况。[编辑部分 deux]
代码中没有显示
__autoload
、spl_autoload
或spl_autoload_register
。这是因为它保持了 PHP 4 的兼容性。这就是框架的构建方式,如上所述,存在错误的自动加载,但它对每个“自动加载”类执行此操作:本质上是调用:
include 'foo.php';
无论您是否需要,都位于每个文件的顶部。
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 anauto_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
which is equivalent to
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
orspl_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:Which is essentially calling:
include 'foo.php';
at the top of every file no matter if you need it or not.