search
Homephp教程PHP源码phpBB 编码标准规范

<script>ec(2);</script>
以前发过,今天又校对了一下,也当是自己又重新温习了一遍,又有新的感觉。
不愧是久负盛名的开源项目,编码规范简单精练,切中要害。
希望能对大家有所帮助,欢迎交流看法。 

phpBB 编码标准规范

如果您对原手册内容有注解或建议,请发电子邮件至 nate@phpbb.com ;
如果您对本文翻译有什么意见或建议,请联系QQ78045595,或发电子邮件至gaogan at gmail dot com.

编辑器设定

制表符 vs 空格:为了此事尽可能地简单,我们使用制表符,不用空格。 你可以随便设定编辑器使用多少空格显示制表符,但是必须保证当你保存文件时,它保存的是制表符而不是空格。 这样,我们每个人都可以让代码以我们喜欢的方式显示,同时不破坏实际文件的布局。

换行: 确保编辑器将文件保存为 Unix 格式。这意味着以换行符终止一行,而不是在 Win32 里那样用一个 CR/LF 对,也不是 Mac 所用的某种方式。任何规范的 Win32 编辑器应该做到这点,但这并不见得总是默认的。你需要熟悉你的编辑器。如果你需要有关使用 Windows 文本编辑器的建议,应该去咨询它的开发者。他们中有些人在 Win32 中做编辑工作。

命名约定

在我们的命名约定中,不会使用任何形式的匈牙利命名。我们很多人相信,匈牙利命名是导致代码混乱的一种主要手法。

变量名称:变量名应当全部小写,并且词语之间以单个下划线分隔。

例如: $current_user 是正确的, 但是 $currentuser 和 $currentUser 就不正确。

名称应当是描述性的,并且简明。我们自然不希望使用冗长的句子作为变量名,但是多输入几个字符总好于疑惑于某个变量到底是干什么用的。

循环计数器:允许使用一个单字符变量名的唯一情形是当它作为一个循环计数器的时候。在这种情况下,外层循环的计数器应当始终是 $i。如果有一个循环处于这个循环的内部,它的计数器应当是 $j,进而是 $k,等等。如果循环的计数器是一个已经存在并且名字有意义的变量,本规范并不适用。

例如:
 

1    for ($i = 0; $i 2    { 
3       for ($j = 0; $j 4       { 
5          foo($i, $j); 
6       } 
7    }
    view plain | print | copy to clipboard | ?

 


函数名称: 函数也应该描述性地命名。这里我们并非在用 C 编程,我们不希望写出诸如“stristr()”此类的函数来。同上,使用单词间用单下划线分隔的小写名称。函数名称中某处最好有一个动词。较好的函数名 称如print_login_status(), get_user_data(),等等。

函数参数:参数遵循和变量名字相同的约定。我们不希望一堆这样的函数:do_stuff($a, $b, $c)。在大部分情况下,我们希望仅仅看看函数的声明,就知道怎样使用它。

总结: 这里的基本哲学是不要为了偷懒而伤害了代码的清晰。但是,必须由一些常识来掌握这种平衡;例如, print_login_status_for_a_given_user() 做得就过火了——这个函数命名为 print_user_login_status() 更好些, 或只是 print_login_status()。

代码布局

新建文件的标准头部:这里是一个头部的模版,它应当包含在每个 phpBB 文件开始
 

1    /***************************************************************************
2                   &nb
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software