Programmer's Reference Guide
| PHP File 文件格式 |
命名约定
类
Zend Framework 的类命名总是对应于其所属文件的目录结构的,ZF 标准库的根目录是 “Zend/”,ZF 特别(extras)库的根目录是 "ZendX/",所有 Zend Framework 的类在其下按等级存放。
类名只允许有字母数字字符,在大部分情况下不鼓励使用数字。下划线只允许做路径分隔符;例如 Zend/Db/Table.php 文件里对应的类名称是 Zend_Db_Table。
如果类名包含多个单词,每个单词的第一个字母必须大写,连续的大写是不允许的,例如 “Zend_PDF” 是不允许的,而 "Zend_Pdf" 是可接受的。
这些约定为 Zend Framework 定义了一个伪命名空间机制。如果对开发者在他们的程序中切实可行,Zend Framework 将采用 PHP 命名空间特性(如果有的话)。
参见在标准和特别库中类名作为类名约定的例子。 重要: 依靠 ZF 库展开的代码,但又不是标准或特别库的一部分(例如程序代码或不是 Zend 发行的库),不要以 "Zend_" 或 "ZendX_" 开头。
文件名
对于其它文件,只有字母数字字符、下划线和短横线("-")可用,空格是绝对不允许的。
包含任何 PHP 代码的任何文件应当以 ".php" 扩展名结尾,众所周知的视图脚本除外。下面这些例子给出 Zend Framework 类可接受的文件名:
Zend/Db.php
Zend/Controller/Front.php
Zend/View/Helper/FormRadio.php
函数和方法
函数名只包含字母数字字符,下划线是不允许的。数字是允许的但大多数情况下不鼓励。
函数名总是以小写开头,当函数名包含多个单词,每个子的首字母必须大写,这就是所谓的 “驼峰” 格式。
我们一般鼓励使用冗长的名字,函数名应当长到足以说明函数的意图和行为。
这些是可接受的函数名的例子:
filterInput()
getElementById()
widgetFactory()
对于面向对象编程,实例或静态变量的访问器总是以 "get" 或 "set" 为前缀。在设计模式实现方面,如单态模式(singleton)或工厂模式(factory), 方法的名字应当包含模式的名字,这样名字更能描述整个行为。
在对象中的方法,声明为 "private" 或 "protected" 的, 名称的首字符必须是一个单个的下划线,这是唯一的下划线在方法名字中的用法。声明为 "public" 的从不包含下划线。
全局函数 (如:"floating functions") 允许但大多数情况下不鼓励,建议把这类函数封装到静态类里。
变量
变量只包含数字字母字符,大多数情况下不鼓励使用数字,下划线不接受。
声明为 "private" 或 "protected" 的实例变量名必须以一个单个下划线开头,这是唯一的下划线在程序中的用法,声明为 "public" 的不应当以下划线开头。
对函数名(见上面 3.3 节)一样,变量名总以小写字母开头并遵循“驼峰式”命名约定。
我们一般鼓励使用冗长的名字,这样容易理解代码,开发者知道把数据存到哪里。除非在小循环里,不鼓励使用简洁的名字如 "$i" 和 "$n" 。如果一个循环超过 20 行代码,索引的变量名必须有个具有描述意义的名字。
常量
常量包含数字字母字符和下划线,数字允许作为常量名。
常量名的所有字母必须大写。
常量中的单词必须以下划线分隔,例如可以这样 EMBED_SUPPRESS_EMBED_EXCEPTION 但不许这样 EMBED_SUPPRESSEMBEDEXCEPTION。
常量必须通过 "const" 定义为类的成员,强烈不鼓励使用 "define" 定义的全局常量。
| PHP File 文件格式 |
Add A Comment
Please do not report issues via comments; use the ZF Issue Tracker.
If you have a JIRA/Crowd account, we suggest you login first before commenting.

Comments
"In general, abstract classes follow the same conventions as classes, with one additional rule: abstract class names must end in the term, "Abstract", and that term must not be preceded by an underscore. As an example, Zend_Controller_Plugin_Abstract is considered an invalid name, but Zend_Controller_PluginAbstract or Zend_Controller_Plugin_PluginAbstract would be valid names."
In the new Package Zend_cloud and Zend_Http from version 1.11.5 name of abstract classes are like this:
Zend_Http_UserAgent_AbstractDevice
Should it be Zend_Http_UserAgent_DeviceAbstract instead ?
Or am i wrong ?
Thanks for your answer.
public static function _unsetInstance()
{
self::$_registry = null;
}
In ZF2 has been almost everything has been converted to Abstract as first term.
The case
Zend_Controller_Plugin_AbstractPluginis usefull if you plan to have a folder for your concrete implementations, for example Plugins.The case
Zend_Controller_AbstractPluginseems only usefull in places where you implement the concrete classes otherwise. Wich for Plugins, it is not.I couldn't find ZF2 conventions yet, but i'll continue searching the tracker and report that to be fixed here.