Home > Article > Backend Development > PHP writing specifications (1)_PHP tutorial
PHP writing standards
Author: sink
Last modified: 2011-7-7
Reference:
PHP Manual
http://www.php.net/manual/zh/language.oop5.basic.php
PEAR Coding Standards
http:/ /pear.php.net/manual/en/standards.php
C++ Coding Standard
http://www.possibility.com/Cpp/CppCodingStandard.html
Google C++ Style Guide
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml
Code Conventions for the Java
http:// www.oracle.com/technetwork/java/codeconvtoc-136057.html
General principles:
1. Semanticization
When you see the name, you know the meaning.
2. Common prefix
is means whether, get means reading, and set means writing. is is followed first by adjectives rather than nouns. For example, if the text is multilingual, is_multilingual should be used instead of is_multilanguage.
3. Singular and plural
Refer to the js function naming rules: getElementById, getElementsByTagName, getElementsByName.
For example:
To get the names of multiple friends of mine, you should use getFriendsName instead of getFriendNames or getFriendName
To get a user, getUser
To get multiple users, getUsers
4. Redundant suffixes
Try not to use data, list, and info suffixes.
For example, pay attention to the naming of js, use getElementsByTagName instead of getElementsInfoByTagName.
You should use getFriends or getFriendsUserId instead of getFriendsList; you should use getUser instead of getUserInfo or getUserData.
But sometimes it is difficult to avoid it. For example, there are two functions, one is to get the user's basic information, and the other is to get the user's detailed information.
Get the user’s basic information: nickname, avatar URI, function name getUserBasic or getUserBasicInfo? It feels inappropriate for the function name to end with an adjective and needs to be discussed.
Get user details: nickname, avatar URI, signature, birthday, function name getUser is no problem.
5. Ambiguous class names, file names, and directory names
Whenever common, util, functions, class, object, and basic are used as file names, Be careful, because these words are too general, there may be more and more things in them as they develop, and they may become a trash can. Give these an accurate name. For example, a class that does string processing can be called StringLib.php and placed in the lib directory.
6. The difference between lib, plugin and addon
Some classes and functions are counted as lib, plugin or addon. To be discussed.
Class name:
Start with a capital letter and use camel case. Generally use nouns, such as configuration parsing class ConfigParser instead of ParseConfig.
Consistent with Java and C++.
For example: class UserModel
The file name of the class:
is the same as the class name. This is related to php autoload. For autoload, the class name must always be very long, to be discussed.
Consistent with Java.
For example: the file name of class UserModel is UserModel.php
Non-class file name:
All lowercase, separated by underscores, no spaces allowed. For example get_user.php.
Directory name:
All lowercase, separated by underscores, no spaces allowed. Such as model, www.
Function name:
Start with a lowercase letter and be named in camel case, for example: function addBlog().
Same as Java and C++.
Function represents a function, that is, an action, so the verb takes precedence. For example, use editBlog instead of blogEdit.
Due to historical reasons, PHP's built-in functions have many styles, such as do_something, something_do, dosomething. The newer functions use doSomething to be consistent with the current mainstream languages.
For example: paser_str, json_encode, substr, fetchAll.
Historical reasons may not be changed, but we can ensure that the new code is rigorous and do not let ourselves become historical reasons.
Function in class:
There is a blank line between the two functions. If you have time, sort the functions alphabetically to avoid too much confusion.
For example:
<ol class="dp-c"> <li class="alt"><span><span class="keyword">class</span><span> BlogModel </span></span></li> <li><span>{ </span></li> <li class="alt"> <span> </span><span class="keyword">public</span><span> </span><span class="keyword">function</span><span> addBlog() </span> </li> <li><span> { </span></li> <li class="alt"><span> } </span></li> <li> <span> </span><span class="keyword">public</span><span> </span><span class="keyword">function</span><span> updateBlog() </span> </li> <li class="alt"><span> { </span></li> <li><span> } </span></li> <li class="alt"><span>} </span></li> </ol>
File comment:
The comment immediately follows the next line of
The format follows the requirements of PHPdoc:
http://manual.phpdoc.org/HTMLframesConverter/default/phpDocumentor/tutorial_tags.author.pkg.html
- /**
- * blog的各种业务:添加、更新
- * @author sink
- *
- */
- class BlogModel
- {
- }
- ?>
API notes:
Be sure to write the input parameters and output format. Write clearly what is output when it is correct and what is output when it is wrong.
Otherwise, others cannot use it.
1