Home > php教程 > php手册 > PHP命名规范

PHP命名规范

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 19:40:21
Original
2296 people have browsed it

以下文字全部摘自《PHP从入门到精通》这本书,谨以此作为标准。 就一般约定而言,类、函数和变量的名字应该是能够让代码阅读者能够容易地知道这些代码的作用,应该避免使用凌磨两可的 命名 。 1. 类 命名 使用大写字母作为词的分割,其他的字母均使用小写。

以下文字全部摘自《PHP从入门到精通》这本书,谨以此作为标准。

就一般约定而言,类、函数和变量的名字应该是能够让代码阅读者能够容易地知道这些代码的作用,应该避免使用凌磨两可的命名

1. 类命名

  • 使用大写字母作为词的分割,其他的字母均使用小写。
  • 名字的首字母使用大写。
  • 不要使用下划线('_')。

如:Name、SuperMan、BigClassObject。

2. 类属性命名

属性名命名采用驼峰命名法(首字母小写,后续单词首字母大写),私有属性使用'_'开始。如:$tablePrefix,$_tablePrefix

3. 类方法命名

方法的作用都是执行一个动作,达到一个目的。所以名称应该说明方法是做什么的。一般名称的前缀都是有第一规律的,如is(判断)、get(得到),set(设置)。

方法的的命名采用驼峰命名法(首字母小写,后续单词首字母大写), 如:getUserName()\ parseLayout()

4. 方法中参数命名

参数命名采用驼峰命名法(首字母小写,后续单词首字母大写)。如

<code>class EchoAnyWord{
    function echoWord($firstWord, $secondWord){
        ...
    }
}</code>
Copy after login

5. 变量命名

  • 所有字母都使用小写。
  • 使用‘_’作为每个词的分界

如:$msg_error、$chk_pwd等。

6. 引用变量

引用变量要带有‘r’前缀。如:

<code>class Example{
    $mExam = "";
    funciton SetExam(&$rExam){
        ...
    }
    function $rGetExam(){
        ...
    }
}</code>
Copy after login

7. 全局变量

全局变量应该带有前缀‘g’。如:global = $gTest、global = $g。

8. 常量、全局常量

常量、全局常量,应该全部使用大写字母,单词之间用‘_’来分割。如

<code>define('DEFAULT_NUM_AVE',90);
define('DEFAULT_NUM_SUM',500);</code>
Copy after login

9. 静态变量

静态变量应该带有前缀‘s’。如:

<code>station $sStatus = 1;</code>
Copy after login

10. 函数命名

所有的名称都使用小写字母,多个单词使用‘_’来分割。如:

<code>function this_good_idear(){
    ...
}</code>
Copy after login

参考:PHP命名规则

Related labels:
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template