Home > Backend Development > PHP Tutorial > Detailed introduction to php naming rules

Detailed introduction to php naming rules

WBOY
Release: 2016-07-25 09:00:07
Original
1376 people have browsed it
  1. class NameOneTwo
  2.    class Name
Copy code

2. Naming of class library

Currently, namespaces are being used more and more widely to avoid class name conflicts between class libraries of different vendors and groups. When namespaces have not yet been adopted, in order to avoid class name conflicts, the general approach is to add a unique prefix before the class name. Two characters is enough. Of course, it would be better to use more. 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

  Adopt the same rules as class naming, and also use English words to explain the function of the method.​ For example:

  1. class NameOneTwo
  2. Naming
  3.  Attribute names should be prefixed with the character ‘m’. The prefix 'm' follows a consistent rule for class naming. 'm' always modifies the beginning of a name, just as 'r' represents a reference. The 'm' prefix prevents any conflict between class attribute and method names. Your method names and property names will often be very similar, especially when accessing elements.​ For example:
class NameOneTwo

   {

   function VarAbc() {};

   function ErrorNumber() {};

   var mVarAbc;
  var mErrorNumber  
var mrName;
       }
  1. Copy code
  2. 5. Naming of parameters in methods
  3.   Use lowercase letters for the first character. All words after the first character are capitalized according to class naming rules.​ This way you can know which variable corresponds to which variable at any time. In addition, this allows you to use a name similar to the class name without causing a name conflict. For example:
class NameOneTwo

6. Naming of variables
 Use lower case for all letters. Use '_' as a delimiter for each word. Many PHP tutorials say this. In this way, the scope of variables in the code is clear. All variables look different in the code and are easily identifiable.​ For example:
  1. function HandleError($errorNumber)
  2.    {
  3.    $error = OsErr();
   $time_of_error = OsErr->getTimeOfError;
  cessor;   }

Copy Code

7. Naming of global variables
  Global variables should be prefixed with ‘g’. It is very 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 follows the C GNU convention, all letters are lowercase letters, and '_' is used to separate words. This makes it easier to distinguish related class names.​ For example:

  1. function some_bloody_function()
  2.     {
  3.     }
Copy code

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template