CodeIgniter 用户指南 版本 2.2.6

编辑文档、查看近期更改请 登录注册  找回密码
查看原文

常用风格和语法

下面将描述采用CI开发中的编码的规范.

内容列表

文件格式

文件应该使用 Unicode (UTF-8) 编码保存。同时不要使用 字节序标记(BOM) 。与 UTF-16 和 UTF-32 不同,UTF-8 编码的文件不需要指明字节序,而且 字节序标记(BOM) 在PHP中会产生预期之外的输出,阻止了应用程序设置它自己的头信息。应该使用Unix 格式的行结束符(LF)。

以下是在一些常见的文本编辑器中更改这些设置的方法。针对你的编辑器,方法也许会有所不同;请参考你的编辑器的说明。

TextMate
  1. Open the Application Preferences
  2. Click Advanced, and then the "Saving" tab
  3. In "File Encoding", select "UTF-8 (recommended)"
  4. In "Line Endings", select "LF (recommended)"
  5. Optional: Check "Use for existing files as well" if you wish to modify the line endings of files you open to your new preference.
BBEdit
  1. Open the Application Preferences
  2. Select "Text Encodings" on the left.
  3. In "Default text encoding for new documents", select "Unicode (UTF-8, no BOM)"
  4. Optional: In "If file's encoding can't be guessed, use", select "Unicode (UTF-8, no BOM)"
  5. Select "Text Files" on the left.
  6. In "Default line breaks", select "Mac OS X and Unix (LF)"

PHP 闭合标签

PHP闭合标签“?>”在PHP中对PHP的分析器是可选的。 但是,如果使用闭合标签,任何由开发者,用户,或者FTP应用程序插入闭合标签后面的空格都有可能会引起多余的输出、php错误、之后的输出无法显示、空白页。因此,所有的php文件应该省略这个php闭合标签,并插入一段注释来标明这是文件的底部并定位这个文件在这个应用的相对路径。这样有利于你确定这个文件已经结束而不是被删节的。

不当的: <?php echo "Here's my code!"; ?> 适当的: <?php echo "Here's my code!"; /* End of file myfile.php */ /* Location: ./system/modules/mymodule/myfile.php */

类和方法(函数)的命名规则

类名的首字母应该大写。如果名称由多个词组成,词之间要用下划线分隔,不要使用骆驼命名法。类中所有其他方法的名称应该完全小写并且名称能明确指明这个函数的用途,最好用动词开头。尽量避免过长和冗余的名称

不当的: class superclass class SuperClass 适当的: class Super_class class Super_class { function __construct() { } }

不当的和适当的方法名称的示例:

不当的: function fileproperties() // 方法名没有清晰的描述以及下划线分割单词 function fileProperties() // 方法名没有清晰的描述以及使用了驼峰法命名 function getfileproperties() // 还可以!但是忘记了下划线分割单词 function getFileProperties() // 使用了驼峰法命名 function get_the_file_properties_from_the_file() // 方法名太冗长 适当的: function get_file_properties() // 清晰的方法名描述,下划线分割单词,全部使用小写字母

变量命名

变量的命名规则与方法的命名规则十分相似。就是说,变量名应该只包含小写字母,用下划线分隔,并且能适当地指明变量的用途和内容。那些短的、无意义的变量名应该只作为迭代器用在for()循环里。

不当的: $j = 'foo'; // 单字符变量应该只作为for()的循环变量使用 $Str // 使用了大写字母 $bufferedText // 使用了驼峰命名,而且变量名应该更短,并有清晰的语法含义 $groupid // 多个词组,应该使用下划线分割 $name_of_last_city_used // 太长了 适当的: for ($j = 0; $j < 10; $j++) $str $buffer $group_id $last_city

注释

通常,代码应该被详细地注释。这不仅仅有助于给缺乏经验的程序员描述代码的流程和意图,而且有助于给你提供丰富的内容以让你在几个月后再看自己的代码时仍能很好的理解。 注释没有强制规定的格式,但是我们建议以下的形式。

文档块(DocBlock) 式的注释要写在类和方法的声明前,这样它们就能被集成开发环境(IDE)捕获:

/** * Super Class * * @package Package Name * @subpackage Subpackage * @category Category * @author Author Name * @link http://example.com */ class Super_class { /** * Encodes string for use in XML * * @access public * @param string * @return string */ function xml_encode($str)

使用行注释时,在大的注释块和代码间留一个空行。

// break up the string by newlines $parts = explode("\n", $str); // A longer comment that needs to give greater detail on what is // occurring and why can use multiple single-line comments. Try to // keep the width reasonable, around 70 characters is the easiest to // read. Don't hesitate to link to permanent external resources // that may provide greater detail: // // http://example.com/information_about_something/in_particular/ $parts = $this->foo($parts);

常量

常量命名除了要全部用大写外,其他的规则都和变量相同。在适当的时候,始终使用CodeIgniter常量,例如LASH, LD, RD, PATH_CACHE等等.

不当的: myConstant // 未使用下划线分割单词,未全部使用大写字母 N // 不能使用单个字母作为常量 S_C_VER // 常量名没有清晰的含义 $str = str_replace('{foo}', 'bar', $str); // should use LD and RD constants 恰当的: MY_CONSTANT NEWLINE SUPER_CLASS_VERSION $str = str_replace(LD.'foo'.RD, 'bar', $str);

TRUE, FALSE, 和 NULL

TRUE, FALSE, 和 NULL 关键字应该总是完全大写的。

不当的: if ($foo == true) $bar = false; function foo($bar = null) 恰当的: if ($foo == TRUE) $bar = FALSE; function foo($bar = NULL)

逻辑操作符

|| 有时让人底气不足,不容易辨识,因为在某些输出设备上它不够清晰(可能看起来像数字11). && 要优先于 AND ,不过两者都可以被接受, 使用 ! 时要在其前后都加一个空格。

不当的: if ($foo || $bar) if ($foo AND $bar) // 可以,但有时不被常用的语法程序高亮推荐(高亮标识) if (!$foo) if (! is_array($foo)) 恰当的: if ($foo OR $bar) if ($foo && $bar) // 推荐 if ( ! $foo) if ( ! is_array($foo))

比较返回值与类型映射

部分PHP函数执行失败时返回 FALSE, 但也可能有一个有效的返回值 "" 或 0, 它在松散比较中会被计算为FALSE. 在条件语句中使用这些返回值的时候,为了确保返回值是你所预期的类型而不是一个有着松散类型的值,请进行显式的比较。

在返回和检查你自己的变量时也要遵循这种严格的方法,必要时使用===!==

不当的: // 如果 'foo' 位于此字符串的起始处,strpos将返回 0, // 此处条件判断的结果为TRUE if (strpos($str, 'foo') == FALSE) 恰当的: if (strpos($str, 'foo') === FALSE) 不当的: function build_string($str = "") { if ($str == "") // uh-oh! 如果传递的参数是FALSE或者整数0那会怎么样? { } } 恰当的: function build_string($str = "") { if ($str === "") { } }

See also information regarding typecasting, which can be quite useful. Typecasting has a slightly different effect which may be desirable. When casting a variable as a string, for instance, NULL and boolean FALSE variables become empty strings, 0 (and other numbers) become strings of digits, and boolean TRUE becomes "1":

试译:另见类型映射的信息,也会非常有用。类型映射的结果稍微有些不同,但也是可用的。比如,你把一个变量映射为字符串的时候,NULL以及布尔值FALSE会变成空字符串,0(以及其它数字)变成包含数字的字符串,布尔值TRUE变成 "1":

$str = (string) $str; // 将 $str 映射为字符串

调试代码

No debugging code can be left in place for submitted add-ons unless it is commented out, i.e. no var_dump(), print_r(), die(), and exit() calls that were used while creating the add-on, unless they are commented out.

试译:在已提交的附加组件所在的地方不能有调试代码,它们被注释掉的情况除外,例如,创建附加组件时不能调用 var_dump(), print_r(), die(), 以及 exit() ,除非它们已经被注释掉了。

// print_r($foo);

文件中的空格

No whitespace can precede the opening PHP tag or follow the closing PHP tag. Output is buffered, so whitespace in your files can cause output to begin before CodeIgniter outputs its content, leading to errors and an inability for CodeIgniter to send proper headers. In the examples below, select the text with your mouse to reveal the incorrect whitespace.

试译:在PHP开始标记之前和结束标记之后都不能有空格。输出已经被缓存,所以文件中的空格会导致CodeIgniter在输出自己的内容之前就开始了输出,这会使CodeIgniter出错且无法输出正确的header。在下面的例子中,使用鼠标选中这些文本,你就能看到那些不应该有的空格。

不当的:

<?php // ...在PHP开始标记上面有空格和换行符 // 并且在PHP结束标记后面也有空格 ?>

恰当的:

<?php // 本例中,PHP开始标记之前和结束标记之后就没有空格 ?>

兼容性

Unless specifically mentioned in your add-on's documentation, all code must be compatible with PHP version 4.3+. Additionally, do not use PHP functions that require non-default libraries to be installed unless your code contains an alternative method when the function is not available, or you implicitly document that your add-on requires said PHP libraries.

试译:除非你的附加组件的文档中有特别说明,否则所有代码必须与PHP 5.1以上版本兼容。此外,不要使用那些依赖于非默认安装的库的函数,除非你的代码中包含了该函数不可用时的替代方法,或者你在文档中明确说明了你的附加组件需要某些库。

使用常见词语来命名类和文件

When your class or filename is a common word, or might quite likely be identically named in another PHP script, provide a unique prefix to help prevent collision. Always realize that your end users may be running other add-ons or third party PHP scripts. Choose a prefix that is unique to your identity as a developer or company.

试译:当你的类或文件名是一个常见词语时,或者是很可能与另一个PHP脚本同名时,使用一个唯一的前缀来避免冲突。你必须始终明白这一点:你的最终用户可能会运行其它第三方的附加组件或者PHP脚本。选择一个能够唯一标识开发者或公司的前缀。

不当的: class Email pi.email.php class Xml ext.xml.php class Import mod.import.php 恰当的: class Pre_email pi.pre_email.php class Pre_xml ext.pre_xml.php class Pre_import mod.pre_import.php

数据库表名

Any tables that your add-on might use must use the 'exp_' prefix, followed by a prefix uniquely identifying you as the developer or company, and then a short descriptive table name. You do not need to be concerned about the database prefix being used on the user's installation, as CodeIgniter's database class will automatically convert 'exp_' to what is actually being used.

你的附加组件所用到的任何表都必须使用 'exp_' 这个前缀,然后是一个能够唯一标识开发者或公司的前缀,最后才是一个简短的描述性的表名。你不需要担心用户安装时所使用的数据库前缀,因为CodeIgniter的数据库类将根据实际情况自动地对 'exp_' 进行转换。

不当的: email_addresses // 缺少这两个前缀 pre_email_addresses // 缺少 exp_ 前缀 exp_email_addresses // 缺少唯一前缀 恰当的: exp_pre_email_addresses

NOTE: Be mindful that MySQL has a limit of 64 characters for table names. This should not be an issue as table names that would exceed this would likely have unreasonable names. For instance, the following table name exceeds this limitation by one character. Silly, no? exp_pre_email_addresses_of_registered_users_in_seattle_washington

说明: 请注意MySQL对表名的限制是不能多于64个字符。会超出这个限制的那些表名都是不合理的,因此这应该不是问题。例如,下面的这个些表名比最大限制多出一个字符。这很傻,不是吗? exp_pre_email_addresses_of_registered_users_in_seattle_washington

一个文件一个类

Use separate files for each class your add-on uses, unless the classes are closely related. An example of CodeIgniter files that contains multiple classes is the Database class file, which contains both the DB class and the DB_Cache class, and the Magpie plugin, which contains both the Magpie and Snoopy classes.

对于你的附加组件所使用的类应当遵循一个文件一个类的原则,除非这些类是紧密相关的。CodeIgniter的文件中包含多个类的一个例子是数据库类文件,其中包含了DB类和DB_Cache类,还有Magpie插件,其中包含了Magpie和Snoopy类。

空格

Use tabs for whitespace in your code, not spaces. This may seem like a small thing, but using tabs instead of whitespace allows the developer looking at your code to have indentation at levels that they prefer and customize in whatever application they use. And as a side benefit, it results in (slightly) more compact files, storing one tab character versus, say, four space characters.

在代码中使用tab代替空格。这虽然看起来像是小事,但是使用tab代替空格有利于那些阅读你的代码的开发者在他们各自所使用的应用程序中自定义缩进方式。此外还有一个好处是,使用这种方式保存的文件稍微紧凑一点。

换行

文件必须使用Unix换行符保存。这对于那些在Windows下的开发者来说更为重要,但无论如何,确保你的文本编辑器已经设置为使用Unix换行符来保存文件。

代码缩进

使用 Allman 风格缩进。除了类声明以外,括号总是独占一行,且缩进与“属于”它的控制语句同级。

不恰当的: function foo($bar) { // ... } foreach ($arr as $key => $val) { // ... } if ($foo == $bar) { // ... } else { // ... } for ($i = 0; $i < 10; $i++) { for ($j = 0; $j < 10; $j++) { // ... } } 恰当的: function foo($bar) { // ... } foreach ($arr as $key => $val) { // ... } if ($foo == $bar) { // ... } else { // ... } for ($i = 0; $i < 10; $i++) { for ($j = 0; $j < 10; $j++) { // ... } }

方括号及圆括号内的空格符

通常情况下,不要在方括号"[]"和圆括号"()"内增加任何空格符。唯一的例外就是为了提高可读性和区别开它们与函数,在接受参数的PHP语法控制结构所使用的括号里,需要增加空格符(declare, do-while, elseif, for, foreach, if, switch, while)。

不恰当的: $arr[ $foo ] = 'foo'; 正确的: $arr[$foo] = 'foo'; // 数组键值的方括号内没有空格 不恰当的: function foo ( $bar ) { } 正确的: function foo($bar) // 函数声明的圆括号内没有空格 { } 不恰当的: foreach( $query->result() as $row ) // PHP语法控制结构之后有空格,但不是在圆括号内 正确的: foreach ($query->result() as $row)

本地化文本

Any text that is output in the control panel should use language variables in your lang file to allow localization.

所有在控制面板输出的文本都应该使用 lang 文件里的语言变量来允许本地化。

INCORRECT: return "Invalid Selection"; CORRECT: return $this->lang->line('invalid_selection');

私有方法和变量

代码里像一些只是为了被类里其他公开函数所调用而封装的工具/辅助类方法,应该以一个下划线做前缀来命名。

convert_text() // 公开方法 _convert_text() // 私有方法

PHP Errors

Code must run error free and not rely on warnings and notices to be hidden to meet this requirement. For instance, never access a variable that you did not set yourself (such as $_POST array keys) without first checking to see that it isset().

Make sure that while developing your add-on, error reporting is enabled for ALL users, and that display_errors is enabled in the PHP environment. You can check this setting with:

if (ini_get('display_errors') == 1) { exit "Enabled"; }

On some servers where display_errors is disabled, and you do not have the ability to change this in the php.ini, you can often enable it with:

ini_set('display_errors', 1);

NOTE: Setting the display_errors setting with ini_set() at runtime is not identical to having it enabled in the PHP environment. Namely, it will not have any effect if the script has fatal errors

短标记

一直使用 PHP 完整标记,以免服务器不支持短标记,也就是未打开 short_open_tag 。(IT不倒翁注释:这条已经不成立,因为新版本的 CI 已经解决了服务器不支持短标记的问题,不过还是建议使用完整标记)

不正确的: <? echo $foo; ?> <?=$foo?> 正确的: <?php echo $foo; ?>

每行一条语句

切记不要在一行写多条语句

不正确: $foo = 'this'; $bar = 'that'; $bat = str_replace($foo, $bar, $bag); 正确: $foo = 'this';
$bar = 'that';
$bat = str_replace($foo, $bar, $bag);

字符串

一直使用单引号除非你需要解析变量(Jacklee注:CI核心代码未严格遵循此规范),如果需要解析变量请使用大括号, to prevent greedy token parsing. 如果字符串包含单引号的话你可以使用双引号,这样就不用转义了。

INCORRECT: "My String" // 没有解析变量,不需要使用双引号 "My string $foo" // 解析变量需要使用括号 'SELECT foo FROM bar WHERE baz = \'bag\'' // 需要转义单引号''时这样写比较难看,可以使用双引号 CORRECT: 'My String' "My string {$foo}" "SELECT foo FROM bar WHERE baz = 'bag'"

SQL 查询

MySQL的关键词都是大写: SELECT, INSERT, UPDATE, WHERE, AS, JOIN, ON, IN, 等。

考虑到易读性,把长的查询分成多行,最好是每行只有一个从句或子从句。

不正确的: //
关键词是小写并且一行中的查询太长(...表示行的继续)
$query = $this->db->query("select foo, bar, baz, foofoo, foobar as raboof, foobaz from exp_pre_email_addresses ...where foo != 'oof' and baz != 'zab' order by foobaz limit 5, 100");
正确的:
$query = $this->db->query("SELECT foo, bar, baz, foofoo, foobar AS raboof, foobaz
FROM exp_pre_email_addresses
WHERE foo != 'oof'
AND baz != 'zab'
ORDER BY foobaz
LIMIT 5, 100");

缺省函数参数

适当的时候,提供函数参数的缺省值,这有助于防止因错误的函数调用引起的PHP错误,另外提供常见的备选值可以节省几行代码 例如:

function foo($bar = '', $baz = FALSE)

 

翻译贡献者: alsove, bnlt, chenshenghan, crazyfriday, fzdxstf, Hex, ianyang, IT不倒翁, Jacklee, levin, neversaylate, shallow, xxx1052, yinzhili, 杜建宇, 笼中
最后修改: 2014-09-02 15:52:09