多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國(guó)內(nèi)最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁(yè) > php框架 > ZendFramework > Zend Framework教程-Autoloading介紹

Zend Framework教程-Autoloading介紹

來(lái)源:程序員人生   發(fā)布時(shí)間:2014-01-01 11:28:34 閱讀次數(shù):3612次

 自動(dòng)加載是一種機(jī)制,無(wú)需依賴手動(dòng)編寫(xiě)PHP代碼。參考?PHP手冊(cè)自動(dòng)加載,一旦自動(dòng)加載器被定義,你試圖使用一個(gè)沒(méi)有定義的類或接口的情況下,它會(huì)自動(dòng)被調(diào)用。

使用自動(dòng)加載,在項(xiàng)目中你不必?fù)?dān)心類的存放位置。定義一個(gè)良好定義的自動(dòng)加載器,您不需要考慮一個(gè)類文件相對(duì)于當(dāng)前類文件的位置,您只需使用類,自動(dòng)加載器將自動(dòng)查找文件。

此外,自動(dòng)加載,確保只加載一次,提升了性能 -所以可以用它替代require_once()。

Zend Framework 鼓勵(lì)使用自動(dòng)加載,并提供了許多工具實(shí)現(xiàn)自動(dòng)加載代碼庫(kù)以及應(yīng)用程序代碼。下面將介紹這些工具,以及如何有效地使用它們。


自動(dòng)加載的實(shí)現(xiàn)約定

 類命名約定

Zend Framework借鑒了 PEAR的想法,即類名與文件系統(tǒng)的1:1的關(guān)系。簡(jiǎn)單地說(shuō),下劃線字符("_")替換目錄分隔,以代表該文件的路徑,然后添加后綴“.php”。例如,類“Foo_Bar_Baz”將對(duì)應(yīng)文件系統(tǒng)上的"Foo/Bar/Baz.php"。假設(shè)已通過(guò)PHP的include_path設(shè)置類的位置,這使得可以通過(guò) include() 和 require()找到相對(duì)include_path中設(shè)置的路徑查找文件名。

此外,推薦使用供應(yīng)商名稱或項(xiàng)目名稱作為前綴。這意味著,你寫(xiě)的所有的類都有一個(gè)共同的類前綴,例如,Zend Framework的所有代碼前綴為“Zend_”。這種命名約定有助于防止命名沖突。在ZendFramework中,我們經(jīng)常提到“namespace”前綴,要注意不要把它與PHP的本地命名空間混淆。

自動(dòng)加載器設(shè)計(jì)約定

 Zend Framework通過(guò)Zend_Loader_Autoloader實(shí)現(xiàn)支持自動(dòng)加載的,主要提供有以下目標(biāo)和設(shè)計(jì)元素:

提供命名空間匹配。如果類的命名空間前綴是沒(méi)有注冊(cè)的命名空間,會(huì)返回FALSE。
允許定義自動(dòng)加載器作為一個(gè)備用的自動(dòng)加載器。一個(gè)團(tuán)隊(duì)可能分布廣泛,或使用一個(gè)為定義的命名空間前綴情況下,它會(huì)嘗試匹配任何命名空間前綴。但是,這種做法是不推薦,因?yàn)樗赡軙?huì)導(dǎo)致不必要的查找。
允許開(kāi)啟禁止錯(cuò)誤提示。 因此,默認(rèn)情況下,它應(yīng)該處于關(guān)閉狀態(tài)。開(kāi)發(fā)階段,可以啟用它。
可以自定義自動(dòng)加載。一些開(kāi)發(fā)商不希望使用Zend_Loader::loadClass()自動(dòng)加載,但仍想使用Zend Framework的自動(dòng)加載機(jī)制。 Zend_Loader_Autoloader允許使用自定義的自動(dòng)加載。
允許使用SPL自動(dòng)加載回調(diào)鏈。這樣做的目的是允許指定額外的自動(dòng)加載器 。


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

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.





生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 日本japanese18日本护士xxxx | 国产欧美日韩在线观看一区二区三区 | 天天天狠天天透天天制色 | 精品久久一区二区三区 | 国产一级一级片 | 欧美军人男同69gay | 日韩欧美亚洲每日更新网 | 亚洲欧美日韩精品 | 老司机免费午夜精品视频 | 亚洲一区成人 | 91色网站 | 久久视频精品 | 欧美v片 | 国内精品哆啪啪 | 欧美自拍网 | 国产亚洲综合精品一区二区三区 | 成人一区专区在线观看 | 尤物精品 | 欧美α一级毛片 | 亚洲精品九色在线网站 | 日韩欧美一区二区在线观看 | 久久在线免费观看视频 | 欧美日韩午夜视频 | 久久精品国产免费高清 | 亚洲黄色网址在线观看 | 国产精品久久一区 | 日本免费中文字幕在线看 | 波多野结衣啪啪 | 国产一区二区三区在线免费 | 不卡精品国产_亚洲人成在线 | 复仇之路在线观看免费版高清 | 一区二区三区在线播放视频 | 亚洲人成影院在线高清 | 免费看一级毛片欧美 | 2022精品天堂在线视频 | 久久91在线 | 欧美成人免费一区在线播放 | 日韩高清专区 | 日韩欧美国内 | 成人资源在线 | 中文字幕第一页亚洲 |