Tips and style of my php codeing.
Author: Luo Wenchen
This article focuses on 'methodology' instead of practical technology, so this article will not teach you any practical skills and instances. If you are a person who only ask the results, you don't look at the process, please don't waste time to see this article.
*** About PHP and Webpage Code Designs ***
PHP is a very powerful and convenient tool that she can make general parallel design not a very understanding of people to write a general network application, and allow you to do too much planning and documents when writing PHP. ready. Especially if you have a certain degree of C / C , Perl, Java, you can write a very similar approach.
Unfortunately, this is like two sides of a sword. It is easy to write, there is no structure, no structure, not easy to maintain, I am very clear about this, because I have written too much PHP program. This type of program has a name: Quick and Dirty (fast and mess).
Maybe you have started one or several good plans, and try to write some decent programs for the plan. Maybe you are thinking about looking for a better language to replace PHP (such as JSP), but in fact you should know The quality of a program is determined by a pristiner rather than the language used. This is like a sword in the hearts of the martial arts novels. It is unwind to the truth of flowers and grass. Focus, this article is also a relationship with PHP, I just refer to some concepts with PHP.
I am not very clear that the masters think that there is a good program of the original code, but I think a good program is roughly:
1. Named clear 2. Structural control is good 3. Easy to read 4. Note and file description 5. Error and exception processing perfect
The following tips I have to say are actually a general-purpose language, just take PHP to be an example.
*** shrink tip ***
People who have a write process will know that constitutes are one of the basic style and techniques of the writer. The regeneration can make your program easy to read and maintain, and how much is it right, I know There are many people who use Tab to do their contract, and people who have seen my program are very clear that I have always used four blank fonts to make a contract, mainly because I don't like Tab. A blank makes my program very long, but later, many masters have also recommended that you use four blank to be free, (you can see in the C language style and art book The same saying, but I have been using four gaps before I have seen this book. Of course, you can also use Tab to make a contract, I know some people are setting his Editor to make TAB once displayed Four blank spacers are not eight, as for I use 'real four blank font' to replace TAB character, this is to let some viewer see the constituent method will be exactly the same. The disadvantage is probably to occupy a larger disk space, and the resulting source code file will be relatively large. With most Editors, you can freely set Tab, like UltraEditor, PE2 / H, etc.
Conclusion: The four blank regeneration is often better than eight blank (TAB preset).
*** variable naming rules ***
The naming of variables has always been the theme of domestic pristers to explore. In fact, name is a kind of learning, the most famous is like Microsoft Hungar nomenclature, the so-called Hungarian naming method is before the variable name is before Don't distinguish, like:
$ CMYCHAR = "C"; $ smystring = "this is a string"; $ Imyint = 2002; In addition, there are some agreement habits, the most common names like class, such as Class's first The letter is uppercase, the other lowercase, the word and the word is sized by uppercase, and the first letter of Methods is lowercase, the other same class, and some people will add uppercase C language before the Class. Or Class, image:
Class CMYCLASS {// This C Some people say that it is Class, some people say it is C language function mymethod () {...} ...} So you will see some Java Swing Class to distinguish between the same class Class Class to add J: Class JButton {// This j is java ...}
As for the naming of the variable, in addition to Hungarian naming, we are most often seen in Unix typical naming => Plus the bottom line, like:
$ my_baby = "baby"; $ whos_your_daddy = "not me";
However, some people like to use cases to distinguish the way:
$ mybaby = "baby"; $ whosedaddy = "not me";
As for I have seen the other is completely written, you can see this naming in the source of VBD Forum.
The name of the variable is actually very free, and all the rules are just a guidelines in the entire development team. If your plan is written by many people, please try to build a consensus, and use a certain written code and the naming standard must enhance a lot of efficiency.
Try first to look at this program:
$ S = ROUND ($ this-> EY / $ THIS-> LN); $ = $ this-> EY - $ PS / 2 - $ S; $ SV = Round (Max ($ this-> D) / ( $ this-> ln-2)); $ i = 0; While ($ LY> $ this-> SY) {$ i ; imageString ($ this-> iM, $ this-> EF, 8, $ LY-7 $ I * $ SV, $ THIS-> SC); Imageline ($ this-> IM, $ THIS-> SX $ PS, $ LY, $ THIS-> EX-1 $ PS, $ LY, $ THIS- > DC); $ = $ ly - $ s;} I can't do it in half a day. Why is it right?
I don't appreciate such a way of writing. It is very simple, who knows your $ S, $ PS, $ EY, $ SY represents what,
After more than three months, you want to modify the function for your own program, because you
I have already forgotten that the variables are used, so I would rather use some naming ways that looks very cumbersome.
$ Step_Value, $ START_Y's variable name, so I can easily recall at $ step_value at a glance
It is used to represent the value of the interval and the interval, and $ start_y is used to represent the starting Y coordinator. This can make sure I don't
Easy to forget this schedule is what is used.
Look at the other, it is actually the same program code:
$ step = runk ($ this-> end_y / $ this-> line_num); // calculate the spacing of each line $ line_y = $ this-> end_y - $ poly_size / 2 - $ step; // calculation started Y Coordinate $ step_value = round (Max ($ this-> DATA) / ($ this-> line_num-2)); // calculate the value of $ i = 0; While ($ LINE_Y> $ this-> Start_Y ) {$ I ; imageString ($ this-> IM, $ this-> eNG_Font, 8, $ LINE_Y-7, $ I * $ Step_Value, $ this-> str_color); // Write this entitle in accordance with the interval Numerical imageline ($ this-> iM, $ this-> start_x $ poly_size, $ line_y, $ this-> end_x-1 $ potize, $ line_y, $ this-> dash_color); // Draw Turnd $ line_y = $ line_y - $ step;} Is it easy to read? In fact, everyone has a standard in the minds. The name of the letter and variable is actually very liberalization. How to do it is your own choice, I can't guarantee that my way is the best, just provide your own experience, my The principle of change (often) Number is: the most stupid method is often the most useful method.
*** Note and instruction file ***
Adding annotations to you, you can make you or your colleague will be half-metering the time of the program, of course, if this program is not careful, it is easy to let others know how the function is written. Note You can use your own language that you are familiar with yourself, don't necessarily use English, unless your project is to work with some foreigners, PHP supports the way to annotation of C Language type and shell script type, telling C-type I think everyone should be very familiar, but the document does not have a powerful mechanism like Javadoc in PHP, so I suggest that it is good to make an annotation first.
Note in PHP is very similar to C:
You can use / * and * / to package your annotation:
/ * This is a PHP annotation * /
You can also use the // representative to annotate the tail:
Echo "Hello World! / N"; // From this starting is the annotation of PHP. Echo "this is php sample";
Another way is to annotate the #, the role and // of Shell Scripts:
## I also annotate
There are also some people or languages like to do some changes:
/ ** I am a pig head * I am a big pig head * /
/ **************************************** This program is written by the pig , Copyright fingers must be based **************************************************** /
Although it looks very piglet but undeniable, there is a sign-up effect *** structure control ***
The so-called structural control is the rules of IF, for, while, etc. A set of indents under UNIX, looking at those complex parameters you will know that this is not simple, in general, in C's structure, divide the two registrations, one party is C founder (K & R) The use is written, and the other party is the Writing of the GNU, and I personally becomes a relatively good way to write, so it is also only the K & R as a model: you may know that some if's control is written:
IF ($ HIT_POINT <= $ DAMAGE) Echo "you are dead"; Else Echo "you alive"; but I personally extremely opposed such a way of writing, because when IF, you all write this way
Reading is deteriorated, and even which if the IF pair, like this ELSE.
IF ($ A> $ b) $ c = true; if ($ c == true) echo "c = true"; Else Echo "C = false"; ELSE $ C = false;
C's old hand should be very clear that Else is the closest IF pair, but if the program is not a short four lines
This is a thousand lines, such a way of writing is like a land, I don't know when I will detonate.
Therefore, no matter how simple it is to judge, you should add {}, like:
IF ($ HIT_POINT <= $ DAMAGE) {echo "you are dead";
According to this, another example I recommend writing the law:
IF ($ a> $ b) {$ c = true; if ($ c == true) {echo "c = true";} else {echo "c = false";}} else {$ c = false;}
But this program looks bad to it?
In fact, there is a very important skill and concept in the design: don't write if IF written too many nests.
Therefore, the above formula should be changed to a single layer of writing:
IF ($ A> $ b) {$ c = true;} else {$ c - false;} = {c = true ";} else {echo $ c = false";
Is it much better?
As for another so-called single line condition, like:
// This is a wrong demonstration, please see the next description
($ HIT_POINT <= $ DAMAGE)? Echo "you are dead": echo "you are alive";
In the case of a simple position, I don't object to use a single-line condition, but the single-line condition is careful, in general, I suggest that it is in a single-line condition when you need an assignment value, like:
$ alive = ($ HIT_POINT <= $ DAMAGE)? FALSE: TRUE;
Because when you need to assign a value, use IF does not take much cheap, like the above-form formula is written to write: if ($ Hit_point <= $ DAMAGE) {$ alive = false;} else {$ alive = true; // Due to two assignments, it seems that it seems stupid. }
There is also a very good place to return the return value in the function, like:
Function myfunction ($ GUESS_NUM) {IF ($ GUESS_NUM == $ Random_Num) {Return True;} else {return false;}}
Use a single row's judgment will not think about it:
Function myfunction ($ GUESS_NUM) {Return ($ GUESS_NUM == $ Random_NUM)? True: false;}
The single line condition is purely personal, and there is no bad problem. Some people extremely opposed to a single-line condition. I think it is a bit pursuit of architecture to the way to go into the magic. I feel that it is used.
*** Postscript ***
At the time of being scaled, the above experience is just the small experience of the lower experience and self-compliance. You don't have to write a program in my way, because it is very stupid to use this way from me to be binding, I believe there Many Tiantian people don't need this.
The author has recently happened many things, so there is no manuscript, I feel that I can't do little penguins, because the original PHP weekly report is what I have commeminated, but it is not willing to be willing, and I don't know how to know. More, I really don't dare to expose the laughed, so I have always emphasized that all articles are just personal care, not a great discovery or research results.
Also, some people think that the author doesn't like to publish some applications, mainly because those things are found in PHP.NET, RFC or PHP Builder, but the method and concept is the biggest key to influence a person's learning results. . However, if you only like to eat beef noodles instead, I think I will provide some ready-made beef noodles for everyone to eat, this may be the last conceive thing.