php开发的语法规范
- 格式:docx
- 大小:25.77 KB
- 文档页数:12
文件格式文件应该使用Unicode (UTF-8) 编码保存。
同时不要使用字节序标记(BOM) 。
与UTF-16 和UTF-32 不同,UTF-8 编码的文件不需要指明字节序,而且字节序标记(BOM) 在PHP中会产生预期之外的输出,阻止了应用程序设置它自己的头信息。
应该使用Unix 格式的行结束符(LF)。
以下是在一些常见的文本编辑器中更改这些设置的方法。
针对你的编辑器,方法也许会有所不同;请参考你的编辑器的说明。
PHP 闭合标签PHP闭合标签“?>”在PHP中对PHP的分析器是可选的。
但是,如果使用闭合标签,任何由开发者,用户,或者FTP应用程序插入闭合标签后面的空格都有可能会引起多余的输出、php错误、之后的输出无法显示、空白页。
因此,所有的php文件应该省略这个php闭合标签,并插入一段注释来标明这是文件的底部并定位这个文件在这个应用的相对路径。
这样有利于你确定这个文件已经结束而不是被删节的。
INCORRECT: <?php echo "Here's my code!"; ?>CORRECT: <?php echo "Here's my code!";/* End of file myfile.php *//* Location: ./system/modules/mymodule/myfile.php */类和方法(函数)的命名规则类名的首字母应该大写。
如果名称由多个词组成,词之间要用下划线分隔,不要使用骆驼命名法。
类中所有其他方法的名称应该完全小写并且名称能明确指明这个函数的用途,最好用动词开头。
尽量避免过长和冗余的名称不当的:class superclassclass SuperClass适当的:class Super_classclass Super_class { function __construct() { } }变量命名变量的命名规则与方法的命名规则十分相似。
就是说,变量名应该只包含小写字母,用下划线分隔,并且能适当地指明变量的用途和内容。
那些短的、无意义的变量名应该只作为迭代器用在for()循环里。
不当的:$j = 'foo'$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 */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 w hat is// occurring and why can use multiple single-line comments. Try to// keep the width reasonable, around 70 characters is the e asiest to// read. Don't hesitate to link to permanent external resour ces// that may provide greater detail://// /information_about_something/in_particular/ $parts = $this->foo($parts);常量常量命名除了要全部用大写外,其他的规则都和变量相同。
不当的:myConstantNS_C_VER恰当的:MY_CONSTANTNEWLINESUPER_CLASS_VERSIONTRUE, FALSE, 和NULLTRUE, FALSE, 和NULL关键字应该总是完全大写的。
不当的: if ($foo == true) $bar = false; function foo($bar = nu ll)恰当的: if ($foo == TRUE) $bar = FALSE; function foo($bar = NU LL)逻辑操作符||有时让人底气不足,因为在某些输出设备上它不够清晰(可能看起来像数字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))调试代码No debugging code can be left in place for submitted add -ons unless it is commented out, i.e. no var_dump(), prin t_r(), die(), and exit() calls that were used while creat ing 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 i ncorrect 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+. Add itionally, do not use PHP functions that require non-defau lt libraries to be installed unless your code contains an alternative method when the function is not available, o r you implicitly document that your add-on requires said PHP libraries.试译:除非你的附加组件的文档中有特别说明,否则所有代码必须与PHP 4.3以上版本兼容。
此外,不要使用那些依赖于非默认安装的库的函数,除非你的代码中包含了该函数不可用时的替代方法,或者你在文档中明确说明了你的附加组件需要某些库。
使用常见词语来命名类和文件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-on s or third party PHP scripts. Choose a prefix that is u nique to your identity as a developer or company.试译:当你的类或文件名是一个常见词语时,或者是很可能与另一个PHP脚本同名时,使用一个唯一的前缀来避免冲突。
你必须始终明白这一点:你的最终用户可能会运行其它第三方的附加组件或者PHP脚本。
选择一个能够唯一标识开发者或公司的前缀。
不当的: class Email pi.email.php class Xml ext.xml.php class Impo rt 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 a s 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, a s CodeIgniter's database class will automatically convert ' exp_' to what is actually being used.你的附加组件所用到的任何表都必须使用'exp_' 这个前缀,然后是一个能够唯一标识开发者或公司的前缀,最后才是一个简短的描述性的表名。