Naming Conventions The. Class. php class files are all suffixed with. Class. php. They are named in the upper case by using the hump method, for example, Pay. Class. php; the names of the classes are the same as those of the Directory _ file names. For example, the Directory of the class name zend_autoloader is Zend/autoloader. Class. php; the alias function uses lowercase letters and underscores. For example, the get_client_ip method uses the hump method, the first letter is lowercase, or the underscore "_", for example, listcomment (), _ getresource (). Generally, the method starting with an underscore is a private method; the name of the marker attribute uses the hump method. The first letter is lowercase or the underline "_", for example, $ username, $ _ instance. Generally, attributes starting with an underscore are private attributes; A constant named after an uppercase letter and an underscore "_", for example, "home_url ";
Common Terms 1> List nouns (singular), such as listapple, read the apple list at first glance. We do not need to write them as getapples, listapples, or readapples-because get is generally used to read a single data, for example, getapple. if listapples is not added with S, we also know that it is an apple list (ensure that the variable name is shortened as much as possible); 2> Get noun (singular); 3> noun total, indicating the total number of items. For example, expensetotal; 4> found: Indicates whether a value has been found; 5> uccess or OK: whether an operation is successful; 6> done: whether a project is completed; 7> error: whether an error occurs. 8> result: the returned result.
Code Refactoring 1. The code in the function or method body should be controlled within a screen as much as possible. 2. Random deletion of unused methods in the class. 3. Modify the methods in others' classes and sign them. 4. Write a ReadMe file in each module (for complex business instructions or code instructions ). 5. Try to make every class do its own thing, and every function does one thing.
Common Code
Use & or | to simplify operations Before simplification:
$ A = 1; $ B = 0; If (isset ($ A) {$ B = 1; print ($ B. "\ n");} if ($ B! = 0) {print ($ B. "\ n ");}
After simplification:
$ A = 1; $ B = 0; isset ($ A) & ($ B = 1) & print ($ B. "\ n"); $ B = 0 | print ($ B. "\ n ");
Obviously, the code looks more neat and simpler!
When determining "=", we may write "=" as "=". It is difficult to debug such a bug. Therefore, the compiler can determine whether to put constants in front. Before:
$ A = 1; if ($ A = 1) {echo '$ A = 1 ';}
After:
$ A = 1; if (1 = $ A) {echo '$ A = 1 ';}
Obviously, if constants are placed in front, the compiler will be able to judge errors.
Regular format:
$ A = 1; if (1 = $ A) {echo '$ A = 1 ';}
Searching tables Before:
/* Error code: Status 1 is returned for, and the error code is, and 6 returns status 2 */$ error = 4; $ state = 0; if ($ error = 4 | $ error = 5 | $ error = 7 | $ error = 8) {$ state = 1 ;} if ($ error = 1 | $ error = 3 | $ error = 6) {$ state = 2;} echo "$ state \ n ";
After:
/* Error code: Status 1 is returned for, and the error code is, and 6 returns status 2 */$ error = 4; $ state = 0; $ arr = array (4 => 1, 5 => 1, 7 => 1, 8 => 1, 1 => 2, 3 => 2, 6 => 2); isset ($ arr [$ Error]) & ($ state = $ arr [$ Error]); echo "$ state \ n ";
Obviously, the code is more concise, clearer, easier to understand, and faster!
The conclusion is that we want to place some design patterns in common code, but there are too many patterns that are not easy to put. These are just microservices! If you have a better way of writing, you can leave a message.