国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php框架 > ZendFramework > Zend Framework教程-Autoloading介绍

Zend Framework教程-Autoloading介绍

来源:程序员人生   发布时间:2014-01-01 11:28:34 阅读次数:3331次

 自动加载是一种机制,无需依赖手动编写PHP代码。参考?PHP手册自动加载,一旦自动加载器被定义,你试图使用一个没有定义的类或接口的情况下,它会自动被调用。

使用自动加载,在项目中你不必担心类的存放位置。定义一个良好定义的自动加载器,您不需要考虑一个类文件相对于当前类文件的位置,您只需使用类,自动加载器将自动查找文件。

此外,自动加载,确保只加载一次,提升了性能 -所以可以用它替代require_once()。

Zend Framework 鼓励使用自动加载,并提供了许多工具实现自动加载代码库以及应用程序代码。下面将介绍这些工具,以及如何有效地使用它们。


自动加载的实现约定

 类命名约定

Zend Framework借鉴了 PEAR的想法,即类名与文件系统的1:1的关系。简单地说,下划线字符("_")替换目录分隔,以代表该文件的路径,然后添加后缀“.php”。例如,类“Foo_Bar_Baz”将对应文件系统上的"Foo/Bar/Baz.php"。假设已通过PHP的include_path设置类的位置,这使得可以通过 include() 和 require()找到相对include_path中设置的路径查找文件名。

此外,推荐使用供应商名称或项目名称作为前缀。这意味着,你写的所有的类都有一个共同的类前缀,例如,Zend Framework的所有代码前缀为“Zend_”。这种命名约定有助于防止命名冲突。在ZendFramework中,我们经常提到“namespace”前缀,要注意不要把它与PHP的本地命名空间混淆。

自动加载器设计约定

 Zend Framework通过Zend_Loader_Autoloader实现支持自动加载的,主要提供有以下目标和设计元素:

提供命名空间匹配。如果类的命名空间前缀是没有注册的命名空间,会返回FALSE。
允许定义自动加载器作为一个备用的自动加载器。一个团队可能分布广泛,或使用一个为定义的命名空间前缀情况下,它会尝试匹配任何命名空间前缀。但是,这种做法是不推荐,因为它可能会导致不必要的查找。
允许开启禁止错误提示。 因此,默认情况下,它应该处于关闭状态。开发阶段,可以启用它。
可以自定义自动加载。一些开发商不希望使用Zend_Loader::loadClass()自动加载,但仍想使用Zend Framework的自动加载机制。 Zend_Loader_Autoloader允许使用自定义的自动加载。
允许使用SPL自动加载回调链。这样做的目的是允许指定额外的自动加载器 。


//////////////////////////
原文如下:

Goals and Design

Class Naming Conventions

To understand autoloading in Zend Framework, first you need to understand the relationship between class names and class files.

Zend Framework has borrowed an idea from ? PEAR, whereby class names have a 1:1 relationship with the filesystem. Simply put, the underscore character ("_") is replaced by a directory separator in order to resolve the path to the file, and then the suffix ".php" is added. For example, the class "Foo_Bar_Baz" would correspond to "Foo/Bar/Baz.php" on the filesystem. The assumption is also that the classes may be resolved via PHP'sinclude_path setting, which allows both include() and require() to find the filename via a relative path lookup on the include_path.

Additionally, per PEAR as well as the ? PHP project, we use and recommend using a vendor or project prefix for your code. What this means is that all classes you write will share a common class prefix; for example, all code in Zend Framework has the prefix "Zend_". This naming convention helps prevent naming collisions. Within Zend Framework, we often refer to this as the "namespace" prefix; be careful not to confuse it with PHP's native namespace implementation.

Zend Framework follows these simple rules internally, and our coding standards encourage that you do so as well for all library code.

Autoloader Conventions and Design

Zend Framework's autoloading support, provided primarily via Zend_Loader_Autoloader, has the following goals and design elements:

  • Provide namespace matching. If the class namespace prefix is not in a list of registered namespaces, return FALSE immediately. This allows for more optimistic matching, as well as fallback to other autoloaders.

  • Allow the autoloader to act as a fallback autoloader. In the case where a team may be widely distributed, or using an undetermined set of namespace prefixes, the autoloader should still be configurable such that it will attempt to match any namespace prefix. It will be noted, however, that this practice is not recommended, as it can lead to unnecessary lookups.

  • Allow toggling error suppression. We feel -- and the greater PHP community does as well -- that error suppression is a bad idea. It's expensive, and it masks very real application problems. So, by default, it should be off. However, if a developer insiststhat it be on, we allow toggling it on.

  • Allow specifying custom callbacks for autoloading. Some developers don't want to useZend_Loader::loadClass() for autoloading, but still want to make use of Zend Framework's mechanisms. Zend_Loader_Autoloader allows specyfing an alternate callback for autoloading.

  • Allow manipulation of the SPL autoload callback chain. The purpose of this is to allow specifying additional autoloaders -- for instance, resource loaders for classes that don't have a 1:1 mapping to the filesystem -- to be registered before or after the primary Zend Framework autoloader.





生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生