PHP Naming conventions

Source: Internet
Author: User
Tags naming convention

As a general rule, the names of classes, functions, and variables should be able to make it easy for code readers to know what the code does, and avoid using the ambiguous nomenclature.

1. Class naming

    • Use uppercase letters for the segmentation of words, and all other letters in lowercase.
    • The first letter of the name is capitalized.
    • Do not use underline ('_').
    • such as: Name, Superman, Bigclassobject.

2. Class attribute naming

    • Attribute naming should be prefixed with the character ' M '.
    • The prefix ' m ' is followed by a rule that is consistent with the class naming.
    • ' m ' always modifies at the beginning of the name, as if it were a reference to the beginning of ' R '.
    • such as: Mvalue, mlongstring, etc.

3. Naming the method

    • The function of a method is to perform an action to achieve a purpose. So the name should indicate what the method is. The prefix of the general name is the first rule, such as is (judgment), get (Get), set (setting).
    • The first letter of the name of the method is lowercase, followed by the initials of the word: Such as:
1234567 class StartStudy{                       //设置类        $mLessonOne"";               //设置类属性        $mLessonTwo"";               //设置类属性        functiongetLessonOne(){        //定义方法,得到属性mLessonOne的值            ...    }}

4. Parameter naming in methods

    • The first character uses a lowercase letter.
    • All characters after the first character are capitalized by the first character of the class naming convention.
    • Such as:
12345 classEchoAnyWord{    functionechoWord($firstWord,$secondWord){        ...    }}

5. Reference variables

    • Reference variables are prefixed with the ' R ' prefix. Such as:
123456789 classExample{    $mExam"";    funciton setExam(&$rExam){        ...    }    functiongetExam(){        ...    }}

6. Variable naming

    • All letters are used in lowercase.
    • Use ' _ ' as the demarcation of each word.
    • such as: $msg _error, $chk _pwd and so on.
    • Temporary variables are usually named I,j,k,m and N, which are generally used for integer types; c,d,e,s they are generally used for character types.
    • The instance variable is preceded by an underscore, the first lowercase letter, and the remaining words capitalized.

7. Global variables

    • Global variables should be prefixed with ' G '. such as: Global $gTest.

8. Constants, Global constants

    • constants, global constants, should all use uppercase letters, between words with ' _ ' to split. Such as
12 define(‘DEFAULT_NUM_AVE‘,90);define(‘DEFAULT_NUM_SUM‘,500);

9. Static variables

    • Static variables should be prefixed with ' s '. Such as:
1 state $sStatus= 1;

10. Function naming

    • All names use lowercase letters, and multiple words are split with ' _ '. Such as:
123 functionthis_good_idear(){    ...}

The various naming conventions above can be combined together to use, such as:

123 classOtherExample{    $msValue"";        //该参数既是类属性,又是静态变量}

PHP Naming conventions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.