PHP代码式样

Jun 13, 2016 pm 01:15 PM
class function php

PHP代码样式
如果使用第三方框架,并且有提供代码样式说明,则遵循其代码样式标准,否则如下!

== 格式:

* 使用UTF-8编码

* 使用4个空格缩进,禁止使用Tab

* Unix样式换行符(LF)

* 在逗号、冒号和分号操作符使用空格

* 在 (,[ 后面、],) 前面不使用空格

* 在代码块中使用4个空格缩进

* 采用层级缩进

* 在return方法的返回值前使用空行(除非只有一行代码)以及在两个方法中使用空行区分

* 在两个大的逻辑代码段之间使用空行区分

* 保持行字数在80字以内,最多不超过120

* 使用标准的PHP标签定界,禁止使用短标签( //... ?>),对于只包含有PHP代码的文
件,禁止使用PHP结束标志("?>"),文件末尾使用注释说明"/* End of file */"

* 单行代码也必须使用分号(;)结束

== 命名:

* 文件名使用snake_case方式,禁止使用臃肿的文件名

* 变量名使用snake_case方式,禁止使用臃肿的变量名
  * 禁止使用单字符做为局部变量(如$i),在for循环中除外
  * 禁止使用大写字母做为全局变量,如使用大写字母,应使用SCREAMING_SNAKE_CASE方式

* 类名使用CamelCase方式,方法名使用camelCase方式(保持像HTTP,RFC,XML缩写词的大写)

* 常量名使用SCREAMING_SNAKE_CASE方式

// bad
superclass.php
SuperClass.php
superClass.php

$i = "foobar";  // 单字符变量只充许使用在for循环中
$bufferdText   // 驼峰式变量,并且意思可以再精简些
$groupid        // 两个单词之间需要下划线分开
$name_of_last_city_used // 太长

MyConstant       // 应该用下划线并且字母没有全大写
N                     // 单字符
S_C_VER           // 意思不清楚

class superclass
class superClass

function fileproperties() // 意思不清楚并且没有驼峰式命名
function fileProperties() // 意思不清楚
function getfileproperties() // 好些了,但没有驼峰式命名

// good
super_class.php

for ($i = 0; $i < 10; $i++)
$buffer
$group_id
$last_city

MY_CONSTANT
NEWLINE
SUPER_CLASS_VERSION

class SuperClass

function getFileProperties()
Copy after login


== 语法:

对于嵌入HTML中的PHP代码,对于像if, for, foreach, while等代码块,采用if: ... endif; for: ... endfor; foreach: ... endforeach;以及 while: ... endwhile;方法块

...
<?php if ($user->isLoggedIn()): // checking logged in ?>
<!-- HTML goes here. -->
<?php endif; // end checking logged in ?>

<?php foreach ($users as $user): // loop users ?>
<!-- HTML goes here. -->
<?php endforeach; // end loop users ?>
...
Copy after login


== 注释:

* 文档块必须和phpDocumentor格式兼容,请参考: http://phpdoc.org/

* 避免多余的意见

/**
 * 控制器类说明信息
 */
class Controller {
    private static $instance;

    public function __construct()  {
        ...
    }

    /**
     * 函数说明信息
     */
    public static function &get_instance() {
        ...
    }

/* End of file controller.php */
Copy after login


== 其他:

* 保持代码简单

* 保持一致性


参考:CodeIgniter,ZendFramework,Wordpress等
http://codeigniter.com/user_guide/general/styleguide.html
http://framework.zend.com/manual/zh/coding-standard.coding-style.html
http://codex.wordpress.org/WordPress_Coding_Standards

其他
Github https://github.com/styleguide
HTML/CSS 代码样式(英文)
http://google-styleguide.googlecode.com/svn/trunk/htmlcssguide.xml
Javascript 代码样式(英文)
http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml
Ruby 样式参见(英文) https://github.com/chneukirchen/styleguide/blob/master/RUBY-STYLE

1 楼 huawei2772 2012-02-17  
好! PHP代码式样

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles