A detailed introduction to the PHP naming rules

Source: Internet
Author: User
    1. Class Nameonetwo
    2. Class Name
Copy Code

2. Naming of class libraries

Currently, namespaces are being used more and more extensively to avoid conflicting class names between different vendors and groups of libraries.  When namespaces are not yet in place, in order to avoid class name collisions, it is common practice to prefix the class name with a unique, two-character, and certainly a better one. For example:

    1. John Johnson's data structure class library can be prefixed with JJ, as follows:
    2. Class Jjlinklist
    3. {
    4. }
Copy Code

3. Naming of methods

Using rules that are consistent with class naming also uses English words to interpret the method. For example:

    1. Class Nameonetwo
    2. {
    3. function DoIt () {};
    4. function HandleError () {};
    5. }
Copy Code

4. Naming of class attributes

Attribute naming should be prefixed with the character ' M '. The prefix ' m ' is followed by a rule with a consistent class naming.  ' m ' always modifies at the beginning of the name, as if it were a reference to the beginning of ' R '. The prefix ' m ' prevents any conflicts between class properties and method names.   Your method name and attribute name are often very similar, especially for accessing elements. For example:

    1. Class Nameonetwo
    2. {
    3. function Varabc () {};
    4. function ErrorNumber () {};
    5. var mvarabc;
    6. var Merrornumber;
    7. var mrname;
    8. }
Copy Code

5. Naming the parameters in the method

The first character uses a lowercase letter.   All words after the first character are capitalized by the first character of the class naming convention. You can do this at any time to know which variable corresponds to which variable.  In addition, this can be done with a name similar to the class name without conflicting duplicate names. For example:

    1. Class Nameonetwo
    2. {
    3. function Startyourengines (
    4. & $rSomeEngine,
    5. & $rAnotherEngine);
    6. }
Copy Code

6, the name of the variable

All letters are used in lowercase. Use ' _ ' as the demarcation of each word.  In many PHP tutorials, it is said. In this way, the scope of the variables in the code is clear.   All variables look different in the code and are easily recognizable. For example:

    1. function HandleError ($errorNumber)
    2. {
    3. $error = Oserr ();
    4. $time _of_error = oserr->gettimeoferror;
    5. $error _processor = oserr->geterrorprocessor;
    6. }
Copy Code

7. Naming Global variables

Global variables should be prefixed with ' G '.   It is important to know the scope of a variable. For example:

    1. Global $gLog;
    2. Global & $grLog;
Copy Code

8. Naming of functions

The function name uses the C GNU convention, with all letters using lowercase letters, using ' _ ' to split the words.   This makes it easier to distinguish between the associated class names. For example:

    1. function Some_bloody_function ()
    2. {
    3. }
Copy Code
  • 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.