Home Backend Development PHP Tutorial PHP File 文件格式

PHP File 文件格式

Jun 23, 2016 pm 02:30 PM

PHP File 文件格式

对于只包含有 PHP 代码的文件,结束标志("?>")是不允许存在的,PHP自身不需要("?>"), 这样做, 可以防止它的末尾的被意外地注入相应。

重要: 由 __HALT_COMPILER() 允许的任意的二进制代码的内容被 Zend Framework 中的 PHP 文件或由它们产生的文件禁止。 这个功能的使用只对一些安装脚本开放。

缩进由四个空格组成,禁止使用制表符 TAB 。

行的最大长度

一行 80 字符以内是比较合适,就是说,ZF 的开发者应当努力在可能的情况下保持每行代码少于 80 个字符,在有些情况下,长点也可以, 但最多为 120 个字符。

行结束标志

行结束标志遵循 Unix 文本文件的约定,行必需以单个换行符(LF)结束。换行符在文件中表示为 10,或16进制的 0x0A。

注:不要使用 苹果操作系统的回车(0x0D)或 Windows 电脑的回车换行组合如(0x0D,0x0A)。

 

命名约定

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.phpZend/Controller/Front.phpZend/View/Helper/FormRadio.php                
Copy after login

文件名必须遵循上述的对应类名的规则。

函数和方法

函数名只包含字母数字字符,下划线是不允许的。数字是允许的但大多数情况下不鼓励。

函数名总是以小写开头,当函数名包含多个单词,每个子的首字母必须大写,这就是所谓的 “驼峰” 格式。

我们一般鼓励使用冗长的名字,函数名应当长到足以说明函数的意图和行为。

这些是可接受的函数名的例子:

filterInput()getElementById()widgetFactory()                
Copy after login

对于面向对象编程,实例或静态变量的访问器总是以 "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 代码划分(Demarcation)

PHP 代码总是用完整的标准的 PHP 标签定界:

<?php?>
Copy after login

短标签( )是不允许的,只包含 PHP 代码的文件,不要结束标签 (参见 常规)。

字符串

字符串文字

当字符串是文字(不包含变量),应当用单引号( apostrophe )来括起来:

$a = 'Example String';                    
Copy after login

包含单引号(')的字符串文字

当文字字符串包含单引号(apostrophe )就用双引号括起来,特别在 SQL 语句中有用:

$sql = "SELECT `id`, `name` from `people` WHERE `name`='Fred' OR `name`='Susan'";                    
Copy after login

在转义单引号时,上述语法是首选的,因为很容易阅读。

变量替换

变量替换有下面这些形式:

$greeting = "Hello $name, welcome back!";$greeting = "Hello {$name}, welcome back!";                    
Copy after login

为保持一致,这个形式不允许:

$greeting = "Hello ${name}, welcome back!";                    
Copy after login

字符串连接

字符串必需用 "." 操作符连接,在它的前后加上空格以提高可读性:

$company = 'Zend' . ' ' . 'Technologies';                    
Copy after login

当用 "." 操作符连接字符串,鼓励把代码可以分成多个行,也是为提高可读性。在这些例子中,每个连续的行应当由 whitespace 来填补,例如 "." 和 "=" 对齐:

$sql = "SELECT `id`, `name` FROM `people` "     . "WHERE `name` = 'Susan' "     . "ORDER BY `name` ASC ";                    
Copy after login

数字索引数组

索引不能为负数

建议数组索引从 0 开始。

当用 array 函数声明有索引的数组,在每个逗号的后面间隔空格以提高可读性:

$sampleArray = array(1, 2, 3, 'Zend', 'Studio');                    
Copy after login

可以用 "array" 声明多行有索引的数组,在每个连续行的开头要用空格填补对齐:

$sampleArray = array(1, 2, 3, 'Zend', 'Studio',                     $a, $b, $c,                     56.44, $d, 500);                    
Copy after login

关联数组

当用声明关联数组,array 我们鼓励把代码分成多行,在每个连续行的开头用空格填补来对齐键和值:

$sampleArray = array('firstKey'  => 'firstValue',                     'secondKey' => 'secondValue');                    
Copy after login

类的声明

用 Zend Framework 的命名约定来命名类。

花括号应当从类名下一行开始(the "one true brace" form)。

每个类必须有一个符合 PHPDocumentor 标准的文档块。

类中所有代码必需用四个空格的缩进。

每个 PHP 文件中只有一个类。

放另外的代码到类里允许但不鼓励。在这样的文件中,用两行空格来分隔类和其它代码。

下面是个可接受的类的例子: // 459 9506 - 441 9658 下次从这里开始

/** * Documentation Block Here */class SampleClass{    // 类的所有内容    // 必需缩进四个空格}                    
Copy after login

类成员变量

必须用Zend Framework的变量名约定来命名类成员变量。

变量的声明必须在类的顶部,在方法的上方声明。

不允许使用 var (因为 ZF 是基于 PHP 5 的 ),要用 private、 protected 或 public。 直接访问 public 变量是允许的但不鼓励,最好使用访问器 (set/get)。

函数和方法

函数和方法声明

必须用Zend Framework的函数名约定来命名函数。

在类中的函数必须用 private、 protected 或 public 声明它们的可见性。

象类一样,花括号从函数名的下一行开始(the "one true brace" form)。

函数名和括参数的圆括号中间没有空格。

强烈反对使用全局函数。

下面是可接受的在类中的函数声明的例子:

/** * Documentation Block Here */class Foo{    /**     * Documentation Block Here     */    public function bar()    {        // 函数的所有内容        // 必需缩进四个空格    }}                    
Copy after login

注: 传址(Pass-by-reference)是在方法声明中允许的唯一的参数传递机制。

/** * Documentation Block Here */class Foo{    /**     * Documentation Block Here     */    public function bar(&$baz)    {}}                    
Copy after login

传址在调用时是严格禁止的。

返回值不能在圆括号中,这妨碍可读性而且如果将来方法被修改成传址方式,代码会有问题。

/** * Documentation Block Here */class Foo{    /**     * WRONG     */    public function bar()    {        return($this->bar);    }    /**     * RIGHT     */    public function bar()    {        return $this->bar;    }}                    
Copy after login

函数和方法的用法

函数的参数应当用逗号和紧接着的空格分开,下面可接受的调用的例子中的函数带有三个参数:

threeArguments(1, 2, 3);                    
Copy after login

传址方式在调用的时候是严格禁止的,参见函数的声明一节如何正确使用函数的传址方式。

带有数组参数的函数,函数的调用可包括 "array" 提示并可以分成多行来提高可读性,同时,书写数组的标准仍然适用:

threeArguments(array(1, 2, 3), 2, 3);threeArguments(array(1, 2, 3, 'Zend', 'Studio',                     $a, $b, $c,                     56.44, $d, 500), 2, 3);                    
Copy after login

控制语句

if/Else/Elseif

使用 if and elseif 的控制语句在条件语句的圆括号前后都必须有一个空格。

在圆括号里的条件语句,操作符必须用空格分开,鼓励使用多重圆括号以提高在复杂的条件中划分逻辑组合。

前花括号必须和条件语句在同一行,后花括号单独在最后一行,其中的内容用四个空格缩进。

if ($a != 2) {    $a = 2;}                    
Copy after login

对包括"elseif" 或 "else"的 "if" 语句,和 "if" 结构的格式类似, 下面的例子示例 "if" 语句, 包括 "elseif" 或 "else" 的格式约定:

if ($a != 2) {    $a = 2;} else {    $a = 7;}if ($a != 2) {    $a = 2;} elseif ($a == 3) {    $a = 4;} else {    $a = 7;}                    
Copy after login

在有些情况下, PHP 允许这些语句不用花括号,但在(ZF) 代码标准里,它们("if"、 "elseif" 或 "else" 语句)必须使用花括号。

"elseif" 是允许的但强烈不鼓励,我们支持 "else if" 组合。

Switch

在 "switch" 结构里的控制语句在条件语句的圆括号前后必须都有一个单个的空格。

"switch" 里的代码必须有四个空格缩进,在"case"里的代码再缩进四个空格。

switch ($numPeople) {    case 1:        break;    case 2:        break;    default:        break;}                
Copy after login

switch 语句应当有 default。

注: 有时候,在 falls through 到下个 case 的 case 语句中不写 break or return 很有用。 为了区别于 bug,任何 case 语句中,所有不写 break or return 的地方应当有一个 "// break intentionally omitted" 这样的注释来表明 break 是故意忽略的。

注释文档

所有文档块 ("docblocks") 必须和 phpDocumentor 格式兼容,phpDocumentor 格式的描述超出了本文档的范围,关于它的详情,参考:» http://phpdoc.org/。

所有类文件必须在文件的顶部包含文件级 ("file-level")的 docblock ,在每个类的顶部放置一个 "class-level" 的 docblock。下面是一些例子:

每个包含 PHP 代码的文件必须至少在文件顶部的 docblock 包含这些 phpDocumentor 标签:

/** * 文件的简短描述 * * 文件的详细描述(如果有的话)... ... * * LICENSE: 一些 license 信息 * * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) * @license    http://framework.zend.com/license/3_0.txt   BSD License * @version    $Id:$ * @link       http://framework.zend.com/package/PackageName * @since      File available since Release 1.5.0*/                    
Copy after login

每个类必须至少包含这些 phpDocumentor 标签:

/** * 类的简述 * * 类的详细描述 (如果有的话)... ... * * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) * @license    http://framework.zend.com/license/   BSD License * @version    Release: @package_version@ * @link       http://framework.zend.com/package/PackageName * @since      Class available since Release 1.5.0 * @deprecated Class deprecated in Release 2.0.0 */                    
Copy after login

每个函数,包括对象方法,必须有最少包含下列内容的文档块(docblock):

函数的描述

所有参数

所有可能的返回值

因为访问级已经通过 "public"、 "private" 或 "protected" 声明, 不需要使用 "@access"。

如果函数/方法抛出一个异常,使用 @throws 于所有已知的异常类:

@throws exceptionclass [description]                    
Copy after login

来源:http://framework.zend.com/manual/zh/coding-standard.php-file-formatting.html

Statement of this Website
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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

What are Enumerations (Enums) in PHP 8.1? What are Enumerations (Enums) in PHP 8.1? Apr 03, 2025 am 12:05 AM

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles