PHP coding specifications

Source: Internet
Author: User

This is a previous article, and I feel very good at sorting it out again. We hope to make everyone better.

1. Introduction

1.1. Importance of standardization **

In some aspects, standardization makes everyone feel at the same position. This helps to make these suggestions evolve in many projects, and many companies have spent a lot of weeks arguing by word. Standardization is not a special personal style. It is completely open to local improvements.

 

1.2. Advantages

When a project tries to comply with public standards, it has the following benefits:

· Programmers can understand any code and find out the program status

· Newcomers can quickly adapt to the environment

· Prevent new php contacts from creating a new style and forming a lifelong habit out of time-saving needs

· Prevent new php contacts from making the same mistake again and again

· In a consistent environment, people can reduce their chances of making mistakes.

· Programmers have the same enemy

1.3. Disadvantages

· Standards are determined by people who do not know php, so they usually look silly.

· Because standards are different from what I do, they usually look silly.

· Standards reduce creativity

· Standards are not necessary for long-term mutual cooperation

· The standard forces too many formats

1.4. Discussion

The experiences of many projects draw the conclusion that adopting programming standards can make the project more smooth. Is standard the Key to Success? Of course not. But they can help us, and we need all the help we can get! To be honest, most of the arguments about a standard of detail come from self-confidence. If a reasonable standard is rarely determined to be regarded as a lack of technology **, it is only the reason for taste. Therefore, you must be flexible in controlling your own ideas. Remember that any project depends on the efforts of the team.

1.5. Explanation

1.5.1. Standard Implementation

First, you should find all the most important elements within the development team. Maybe the standards are not appropriate for your situation. It may have summarized important issues, and some may have expressed strong opposition to some of them. No matter under what circumstances, as long as the final event goes smoothly, people will understand that this standard is reasonable, and other programmers will find it reasonable **, and I think it is worthwhile to follow this standard with some reservation. If there is no voluntary cooperation, you can set the requirements: standards must be tested by code. If it is not tested, this solution is just a group of ridiculous people built on an inaccurate Foundation.

1.5.2. agree with the opinion

1. This does not work;

2. It may be feasible, but it is neither practical nor boring;

3. This is true, and I have told you;

4. This is what I first thought;

5. This should have been the case.

If you look at things with negative ideas, please keep your mind open. You can still make a nonsense conclusion, but the method of making a conclusion is that you must be able to accept different ideas. Please give yourself some time to do it.

1.5.3. four phases of the project

1. Database Structure

2. Design

3. Data Layer

4. HTML Layer

 

2. Naming rules

 

2.1. Proper Name

 

Naming is the core of program planning. The ancients believed that as long as they knew the real name of a person, they would gain incredible power over that person. As long as you give things a correct name, it will give you and people later more powerful than the code. Don't laugh!

The name is a long-term and profound result of a thing in its ecological environment. In general, only programmers who know the system can obtain the most appropriate name for the system. If all the names are suitable for their nature, the relationship is clear, and the meaning can be deduced. The assumption of the average person can also be expected.

If you find that only a few of your names can match their corresponding items, you 'd better look at your design again.

 

2.2. Class Name

 

· Before naming a class, you must first know what it is. If you still cannot remember what the class is, your design is not good enough.

· A hybrid name composed of more than three words is easy to cause confusion between various entities of the system. Let's look at your design and try to use (CRC Session card) check whether the object corresponding to the name has so many functions.

· Naming a derived class should avoid the temptation to include its parent class name. The name of a class is only related to itself, and has nothing to do with its parent class name.

· Sometimes suffix names are useful. For example, if your system uses an agent, you can name a part as a "DownloadAgent" for real information transfer.

 

2.3. Method and function naming

 

· Every method and function usually execute an action. Therefore, their names should clearly indicate what they do: replace ErrorCheck () with CheckForErrors () and DumpDataToFile () replace DataFile (). In this way, functions and data can also be differentiated.

· Sometimes suffix names are useful:

O Max-indicates the maximum value that an object can assign.

O Cnt-the current value of a running count variable.

O Key-Key value.

For example, RetryMax indicates the maximum number of retries, and RetryCnt indicates the current number of retries.

· Sometimes prefix names are useful:

O Is-it means to ask a question about something. At any time, people will know that this Is a problem.

O Get-Indicates obtaining a value.

O Set-meaning to Set a value

For example, IsHitRetryLimit.

 

2.4. Do not use uppercase letters for all acronyms

 

· In any of the following situations, you can use the upper-case letters and the lower-case letters in the first letter to represent the acronym.

Use: GetHtmlStatistic.

Not used: GetHTMLStatistic.

Reason

· When the name contains acronyms, people seem to have a very different intuition. Unified Rules are the best, so that the meaning of the name can be completely pre-known.

Here is an example of NetworkABCKey. It is very confusing to note whether C in ABC or C in key. Some people don't care about this, but others hate it. So you will see different rules in different codes, so that you do not know how to call them.

For example

Class FluidOz // do not write it as FluidOZ

Class GetHtmlStatistic // do not write it as GetHTMLStatistic

 

2.5. Class Name

 

· Use uppercase letters as the separation of words, and Use lowercase letters for other letters

· Use uppercase letters for the first letter of the name

· Do not use underscores (_)

Reason

· According to many naming methods, most people think this is the best way.

For example

Class NameOneTwo

Class Name

 

2.6. Class Library name

 

· Namespace is becoming more and more widely used to avoid class name conflicts between different vendors and group class libraries.

· When namespace is not used yet, to avoid class name conflicts, the general practice is to add a unique prefix before the class name so that two characters can be used. Of course, it is better to use more.

For example

John Johnson's Data Structure class library can use Jj as the prefix, as shown below:

Class JjLinkList

{

}

Another compromise is to create a directory containing the class library (in fact, Java does the same). Different directories represent different namespaces.

For example

Microsoft database-related class libraries can be found:

/Classes/com/Microsoft/Database/DbConn. php

Apache database-related class libraries can be found in:

/Classes/org/apache/Database/DbConn. php

 

2.7. Method naming

 

· Adopt rules consistent with the class naming rules

Reason

· Most people who use all different rules find this the best compromise.

For example

Class NameOneTwo

{

Function DoIt (){};

Function HandleError (){};

}

 

2.8. Category ** name

 

· The name of the genus ** should be prefixed with the character 'M.

· The prefix 'M' is followed by the same class naming rules.

· 'M' always acts as a modifier at the beginning of a name, just as if it was referenced at the beginning of 'R.

Reason

· The prefix m prevents any conflict between the class owner ** and the method name. Your method name and owner ** are often very similar, especially the access element.

For example

Class NameOneTwo

{

Function VarAbc (){};

Function ErrorNumber (){};

Var $ mVarAbc;

Var $ mErrorNumber;

Var $ mrName;

}

 

 

2.9. Name PARAMETERS IN THE METHOD

 

· The first character must contain lowercase letters.

· All words after the first character are capitalized according to the class naming rules.

Reason

· Differentiate General variables in a method.

· You can use a name that is similar to a class name Without Duplicate name conflicts.

For example

Class NameOneTwo

{

Fu

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.