日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php框架 > ZendFramework > Zend Framework教程-Autoloading介紹

Zend Framework教程-Autoloading介紹

來源:程序員人生   發布時間:2014-01-01 11:28:34 閱讀次數:3639次

 自動加載是一種機制,無需依賴手動編寫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.





生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 99精品一区二区 | 91精品中文字幕一区二区三区 | av簧片| 国产精品无码永久免费888 | 免费黄色电影在线观看 | 久久97视频| 伊人2 | 欧美一区二区精品 | 在线免费a视频 | 污视频网站在线观看 | 中文精品视频 | 天天干人人 | 欧美日韩亚洲综合 | 国产免费小视频 | 亚洲美女视频一区 | 欧美理论片在线 | 欧美精品一区二区久久 | 精品91久久 | av2014天堂网 | 在线观看免费黄视频 | 麻豆黄网| 爱综合 | 日本一区二区三区在线观看 | 国产精品国产成人国产三级 | swag国产精品一区二区 | 成人福利| 国产欧美综合一区二区三区 | 日韩视频一区 | 欧美日韩电影一区二区 | 黄色成人在线观看 | 91视频在线观看视频 | 久久久久久久国产精品 | 亚洲一区在线观看视频 | 成人在线视频一区 | 国产精品成 | 亚洲六月丁香色婷婷综合久久 | 国产亚洲女人久久久久毛片 | 波多野结衣av电影在线观看 | 免费av高清 | 久久成人高清 | 国产第一亚洲 |