Summary of naming conventions for PHP programming

Source: Internet
Author: User
Tags php programming split words uppercase letter
This article introduces, PHP programming in the naming standard, including the basic concept, camel-style nomenclature, hump method, Pascal method, the need for friends, may wish to refer to the study.

Camel-like nomenclature (also called Hump nomenclature), as its name CamelCase implies, refers to the mixing of uppercase and lowercase letters to form the names of variables and functions. Programmers in order to their own code can be easier to communicate between peers, so more than a unified readability of the better way to name. For example: Some programmers like all lowercase, some programmers like to underline, so if you want to write a variable of my name, they often use the wording of myname, my_name, myname or myname. Such a naming convention is not suitable for all programmers to read, and the use of the Camel name method to express, can increase the readability of the program. For example, the following is the same function that is named with the camel-named method and the underscore method, respectively:

Printemployeepaychecks (); Print_employee_paychecks ();

The first function name uses camel-named notation-each logical breakpoint in the function name is marked with an uppercase letter, and the second function name uses the underscore method----each logical breakpoint in the function name is marked with an underscore. The underline is popular after the advent of C, and it is very common in many of the older programs and UNIX environments.

Camel-style nomenclature (camel-case) is a set of naming conventions (conventions) when a computer program is written. Camel-style nomenclature is when a variable name or function name is a unique identifier consisting of one or more words, the first word starts with a lowercase letter, the first letter of the second word is capitalized, or the first letter of each word is in uppercase letters, for example: MyFirstName, Mylastname, Such variable names look like camel peaks, so they get their name.

The term camel-named (camel-case) is a camel that comes from the widely used case-and-write format in Perl, and the cover picture of Programming Perl (O ' Reilly), a bestseller of Larry Wall. The naming rules of camel-like nomenclature can be regarded as a convention, and there is no absolute and mandatory, in order to increase recognition and readability.

The Hump method (Small hump method) variable is usually identified by the small hump method. The Hump method means that in addition to the first word, the first letter of the word is capitalized. For example:

int mystudentcount;

Variable Mystudentcount The first word is all lowercase and the first letter of the following word is capitalized.

Pascal method (Large hump method) compared to the small hump method, the big hump method of the first word of the initial letter is also capitalized. Commonly used for class names, function names, properties, namespaces. For example:

Publicclass Databaseuser;

Attached, the proper naming of the PHP naming rules is the core of program planning.

A name is a long and far-reaching result of things in the ecological environment in which they are located. In general, only the programmer who understands the system can get the most appropriate name for the system. If all the names are compatible with their nature, then the relationship is clear, meaning can be deduced, and the general presumption can be expected.

If you find that only a small amount of your name can match its counterpart, it's best to take a good look at your design again.

The class name must first know what it is before it is named for the class (class). If you still can't think of what this class is like by providing clues to the class name, then your design is not good enough.

A mix of more than three words is prone to confusion among entities of the system, and then look at your design and try to use the (CRC se-ssion card) to see if the entity that named it has so many functions.

The name of the derived class should avoid the temptation to take its parent class name, which is related only to itself and not to what its parent class is called.

Sometimes the suffix is useful, for example: If your system uses a proxy (agent), then a part is named "Download Agent" (downloadagent) to actually transmit the information. Method and function naming usually each method and function performs an action, so naming them should clearly explain what they do: Replace Errorcheck () with Checkforerrors () and Dumpdatatofile () instead of datafile (). Doing so can also make the function and data a more distinguishable object.

Sometimes the suffix is useful: max-meaning the maximum value that an entity can give. Cnt-The current value of a running count variable. Key-value. For example: Retrymax represents the maximum number of retries, and retrycnt represents the current number of retry attempts.

Sometimes the prefix name is useful: is-meaning to ask a question about something. Whenever people see is, they know it's a problem. Get-meaning to get a numeric value. Set-meaning to set a value for example: Ishitretrylimit.

Abbreviations do not use capital letters in any case, you can use the first letter to capitalize the remaining lowercase letters in lieu of all uppercase words to represent abbreviations.

Use: gethtmlstatistic. Not used: gethtmlstatistic.

Reason when naming contains acronyms, people seem to have very different instincts. Uniform rules are the best, so that the meaning of naming can be fully predictable. As an example of networkabckey, note that C is the C in the ABC or in the key, which is very confusing. Some people don't care about this, others hate it. So you'll see different rules in different code, so you don't know how to call it.

For example

Class Fluidoz//Do not write Fluidozclass gethtmlstatistic//Do not write gethtmlstatistic

Class naming uses uppercase letters as the separation of words, and the other letters are capitalized using the first letter of the lowercase name instead of the underscore (\ ' _\ ') reason is based on many naming methods, most people think this is the best way. For example

Class Nameonetwo

The Class name class library naming the current namespace is being used more and more widely to avoid conflicting class names between different vendors and community 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, John Johnson's data structure class library can be prefixed with JJ, as follows:

Class jjlinklist{}

Method naming using rules that are consistent with class naming the majority of people who use all the different rules find this to be the best compromise. For example

Class Nameonetwo{function DoIt () {};function HandleError () {};}

Class Property naming property 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 reason 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

Class Nameonetwo{function Varabc () {};function errornumber () {};var mvarabc;var merrornumber;var mrName;}

The first character in a method is named with a lowercase letter. All words after the first character are capitalized by the first character of the class naming convention. Reason you can always know that variable corresponds to that variable. You can use a name that is similar to the class name to avoid conflicting names. For example

Class Nameonetwo{function Startyourengines (& $rSomeEngine,& $rAnotherEngine);}

Variable name All letters are used in lowercase using \ ' _\ ' as the demarcation of each word. Reason through this approach, the scope of the variables in the code is clear. All variables look different in the code and are easily recognizable. For example

function HandleError ($errorNumber) {$error = Oserr (); $time _of_error = oserr->gettimeoferror; $error _processor = Oserr->geterrorprocessor;}

Reference variables and functions return reference references must be prefixed with ' r ' to make variables of different types easily recognizable it can determine which method returns the object that can be changed and which method returns the immutable object. For example

Class Test{var Mrstatus;function dosomething (& $rStatus) {};function &rstatus () {};}

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

Global $gLog, Global & $grLog;

Define naming/Global constants global constants separate each word with \ ' _\ '. Reason this is the tradition of naming global constants. You should be careful not to conflict with other definitions. For example

Define ("A_global_constant", "Hello world!";

Static variable static variables should be prefixed with ' s '. Reason to know the scope of a variable is very important. For example

function test () {static $msStatus = 0;}

function naming function name takes c GNU Convention, all letters use lowercase letters, use \ ' _\ ' to split words. Reason this makes it easier to distinguish between the associated class names. For example

function Some_bloody_function () {}

The error return detection rule checks all system calls for error messages unless you want to ignore the error. Define the system error text for each system error message to include.

The braces {} rule is acceptable for two of the three main braces placement rules, as the first of these is best: place curly braces at the same column below the keyword:

if ($condition) while ($condition) {{...}}

The traditional Unix parentheses rule is that the first brackets are the same as the keywords, and the end brackets are the same as the keywords:

if ($condition) {while ($condition) {...}}

The non-principled issues that cause violent controversy can be resolved by compromise, and either approach is acceptable, but for most people the first one is preferred. The reason is that psychology studies the category of learning things. There are more reasons for liking the first. If you use a character editor that supports brace matching (for example, VI), the most important thing is to have a good style. Why? We say when you have a big program and want to know where this big program ends. You first move to the opening parenthesis, and you press the button editor to find the closing parenthesis that corresponds to it, for example:

if ($very _long_condition && $second _very_long_condition) {...} else if (...) {...}

Moving from one block to another requires only the cursor and your parentheses to match the key, so you don't have to move back and forth to the end of the line to find the matching parentheses.

  • 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.