PHP Programming naming rules

Source: Internet
Author: User
Tags functions numeric numeric value variables php programming reference return variable
Programming naming is the core of program planning. The ancients believed that as long as they knew a person's real name, they would gain incredible power over that person.
Amount As long as you think of the right name, it will give you and later people a stronger power than the code. Don't laugh!

A name is a long and far-reaching result in the ecological environment in which things are in place. In general, only the programmer who understands the system can be a department
To take out the most suitable name. If all the names are appropriate to their nature, then the relationship is clear, the meaning can be deduced, the general people's push
To be expected.

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




Class naming
Before you name the class (class), you should know what it is. If you pass the clue provided by the class name, you still can't remember this class is
What, then your design is not good enough.


A mixed name of more than three words is easy to cause confusion between the entities of the system, and then look at your design, try to use (CRC se-
Ssion card) to see if the entity that the name corresponds to has so many functions.


The naming of derived classes should avoid the temptation to bring their parent class names, a class whose name is related only to itself, and what its parent class calls None
Off


Sometimes a suffix name is useful, for example, if your system uses a proxy (agent), then a part is named "Down
Carrier "(downloadagent) for the true transmission of information.
Method and function naming
Usually each method and function performs an action, so naming them should be a clear indication of what they do:
Checkforerrors () replaces Errorcheck () with Dumpdatatofile () instead of datafile (). Doing so can also make the function and
The data becomes a more distinguishable object.


Sometimes the suffix name is useful:
Max-meaning is 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 tests, and retrycnt represents the current number of retries.


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



Abbreviations do not use all uppercase letters
In any case, you can use the first letter to capitalize the rest of the letters in lieu of all uppercase letters in the following situations.
Abbreviated words.

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 the name is done
It's all predictable.
To give a networkabckey example, note that C is supposed to be c in ABC or C in key, which is very puzzling. 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
Go and call it.

For example
Class Fluidoz//Don't write Fluidoz
Class Gethtmlstatistic//Don't write gethtmlstatistic



--------------------------------------------------------------------------------

Class naming
Use uppercase letters as a separate word, and all other letters use lowercase
First letter of first name using uppercase
Do not use underline ('_')
Reason
According to many naming methods, most people think this is the best way.
For example
Class Nameonetwo

Class Name



--------------------------------------------------------------------------------

Class Library naming
Namespaces are being used more and more widely to avoid class name conflicts between different vendor and group library classes.


When namespaces are not yet in place, in order to avoid class name conflicts, the general practice is to precede the class name with a unique prefix, two characters
Yes, of course it would be better to use some more.
For example
John Johnson's data structure class library can be prefixed with JJ, as follows:
Class Jjlinklist
{
}



--------------------------------------------------------------------------------

Method name
Adopt A rule that is consistent with class naming
Reason
Most people who use all the different rules find this to be the best compromise.
For example
Class Nameonetwo
{
function DoIt () {};
function HandleError () {};
}



--------------------------------------------------------------------------------

Class attribute naming
Attribute names should be prefixed with the character ' M '.
The prefix ' m ' is followed by a class-name-consistent rule.
' m ' always modifies the beginning of the name, as if it were a reference at the beginning of ' R '.
Reason
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
Class Nameonetwo
{
function Varabc () {};
function ErrorNumber () {};
var mvarabc;
var Merrornumber;
var mrname;
}



--------------------------------------------------------------------------------

Parameter naming in method
The first character uses a lowercase letter.
All words after the first character are capitalized according to the first character of the class naming rule.
Reason
You can always know that the variable corresponds to that variable.
You can use a name that is similar to a class name without conflicting duplicate names.
For example
Class Nameonetwo
{
function Startyourengines (
& $rSomeEngine,
& $rAnotherEngine);
}



--------------------------------------------------------------------------------

Variable naming
Use lowercase for all letters
Use ' _ ' as the dividing line for each word.
Reason
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
function HandleError ($errorNumber)
{
$error = Oserr ();
$time _of_error = oserr->gettimeoferror;
$error _processor = oserr->geterrorprocessor;
}



--------------------------------------------------------------------------------

Reference variables and function return reference
Reference must be prefixed with ' R '
Reason
Making variables of different types easy to identify
It determines 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
It is very important to know the scope of a variable.
For example
Global $gLog;
Global & $grLog;



--------------------------------------------------------------------------------

Defining a named/global constant
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 variables
Static variables should be prefixed with ' s '.
Reason
It is very important to know the scope of a variable.
For example
function test () {static $msStatus = 0;
}



--------------------------------------------------------------------------------

function naming
The function name uses the C GNU Convention, all letters use the lowercase letter, uses ' _ ' to divide the word.
Reason
This makes it easier to distinguish between the associated class names.
For example
function Some_bloody_function ()
{
}



--------------------------------------------------------------------------------

Error return detection rule
Check all system call error messages unless you want to ignore the error.
The system error text is defined for each system error message for include.


--------------------------------------------------------------------------------

Curly Brace {} rule
Of the three main brace placement rules, there are two acceptable, as the first of the following is the best:
Place curly braces in the same column below the keyword:
if ($condition) while ($condition)
{ {
... ...
} }

The traditional Unix bracket rule is that the first bracket goes with the keyword, and the end bracket is in the same column as the keyword:
if ($condition) {while ($condition) {
... ...
} }

Reason
The problem of the non principle that causes violent controversy can be solved by compromise, and either of the two methods is acceptable, but for the big
Most people prefer the first kind. The reason is the psychological study of the scope of things.
There are more reasons for liking the first one. If you use a character editor that supports bracket matching functions (e.g. VI), the most
The important thing is to have a good style. Why? We say when you have a big chunk of the program and want to know where this chunk of program is knot
Bunch of words. You move to the beginning bracket, and the button editor will find the closing bracket that corresponds to it, for example:

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

Moving from one program block to another requires only the cursor and your parentheses to match the key, and you don't need to move back and forth to the end of the line.
Find a matching bracket.

Related Article

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.