PHP writing specifications PHPCodingStandard_PHP tutorial

Source: Internet
Author: User
PHP writing specification PHPCodingStandard. General principles: 1. you can understand the meaning of a name in semantics. 2. the general prefix "is" indicates whether or not, "get" indicates read, and "set" indicates write. Is followed by adjectives rather than nouns, for example, general principles:
1. Semantic
You can see the name.

2. general prefix
Is indicates whether or not, get indicates read, set indicates write. Is followed by adjectives rather than nouns. for example, if it is a multi-language text, is_multilingual should be used instead of is_multilanguage.

3. singular and plural
Refer to the function naming rules of js: getElementById, getElementsByTagName, and getElementsByName.
For example:
Use getFriendsName instead of getFriendNames or getFriendName to retrieve the names of multiple friends.
GetUser
GetUsers

4. redundant suffixes
Try not to use the suffix data, list, and info.
For example, js naming requires that you use getElementsByTagName instead of getElementsInfoByTagName.
GetFriends or getFriendsUserId should be used instead of getFriendsList; getUser should be used instead of getUserInfo or getUserData.
However, it is sometimes difficult to avoid it. for example, two functions are used to retrieve basic user information and user details.
Get the basic user information: Nickname, profile picture URI, function name getUserBasic or getUserBasicInfo? Function names ending with adjectives are not suitable for discussion.
Get user details: Nickname, profile picture URI, signature, birthday, function name getUser no problem.

5. fuzzy class name, file name, and directory name
Every time you use common, util, functions, class, object, and basic as file names, be cautious. because these words are too common, there may be more and more things in them and they may become garbage bins. To give these accurate names, such as the class for string processing, it can be called StringLib. php and put in the lib directory.

6. differences between lib, plugin, and addon
Some classes and functions are calculated as lib, plugin, or addon. To be discussed.

Class name:
Start with an upper-case letter and name the camper. Generally, nouns are used, such as ConfigParser, rather than ParseConfig.
It is consistent with Java and C ++.
Example: class UserModel

Class File name:
Same as the class name. This is related to php autoload. for autoload, the class name is always long and will be discussed later.
Same as Java.
For example, the file name of class UserModel is UserModel. php.

Non-class File name:
Lowercase letters, underscores (_), and spaces are not allowed. For example, get_user.php.

Directory name:
Lowercase letters, underscores (_), and spaces are not allowed. Such as model and www.

Function name:
Start with a lowercase letter and name it in the hump, for example, function addBlog ().
It is consistent with Java and C ++.
A function indicates a function, that is, an action. Therefore, the verb takes precedence. for example, you can use editBlog instead of blogEdit.
Due to historical reasons, PHP built-in functions have various styles, such as do_something, something_do, and dosomething. the newer functions use doSomething, which is consistent with the current mainstream languages.
For example, paser_str, json_encode, substr, and fetchAll.
Historical reasons may not change, but we can ensure that the new code is rigorous and do not make ourselves the historical reasons.

Functions in the class:
There is a blank line between the two functions. If there is time, the functions are sorted by English letters to avoid confusion.
For example:
Class BlogModel
{
Public function addBlog ()
{

}

Public function updateBlog ()
{

}
}

File notes:
Annotation followed Format according to PHPdoc requirements: http://manual.phpdoc.org/HTMLframesConverter/default/phpDocumentor/tutorial_tags.author.pkg.html
/**
* Various blog services: add and update
* @ 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 incorrect.
Otherwise, it cannot be used by others.

Function notes:
Make sure to write the output format. Write clearly what is output when it is correct, and what is output when it is incorrect.
If the input parameters are complex and contain arrays, you must write a comment on the input parameters.
There cannot be blank lines between document comments and functions.
If the internal steps of the function are complex, you need to write "intra-row comments ".
For example:
/**
* Update a blog
* @ Param int $ id blog_id
* @ Param array $ data array (
"Content" => "", // content
"Tags" => "", // tag
"Update_time" => "", // update time
)
* @ Return bool
*/
Public function updateBlog ($ id, $ data)
{
Step 1 // Step 1: asdf
Step 2 // Step 2: qwer
}

URI:
According to rfc1034 international standards, underlines (_) are not allowed in the domain name. The domain name is case insensitive.
For example, http: // dl_dir.qq.com/is the incorrect domain name.
The http://bkjia.com is the same as the http://VERYHUO.COM.
Therefore, the lower-case URI is preferred, except for the GET value.
For example
Http://www.google.com /? Hl = zh-CN
Http://www.google.com /? Hl = zh-CN
Whether the abbreviation of a non-parameter exclusive term in URI is in lowercase is controversial.
For example
Http://fedoraproject.org/zh_CN/
Http://zh.wikipedia.org/zh-cn/
Http://code.google.com/intl/zh-CN/
Http://www.microsoft.com/en-us/
The language code is a special term. ISO requires a minus sign, and it is recommended that uppercase letters be used for the region.
The usage of fedora is strange. it uses its own zh_CN instead of zh-CN. We do not recommend that you use underscores (_) in the URI.
Wiki uses lower-case letters, google uses upper-case letters, and Microsoft uses lower-case letters.

The minus sign "-" is preferred in the URI, not the underline, except for the GET name.
For example
Http://example.com/1-2-2
Http://example.com /? User_id = 123
If you want to manually enter the URI, it is case-insensitive and the lower case is preferred, because the user input is more convenient.
The actual situation is: users generally manually enter the domain name, rather than manually enter the URI, because the URI is very long. In this case, is URI lowercase meaningful if http://example.com/is used /? UserId = 123, the variable name can use $ userId = $ _ GET ['userid'] to be consistent with Java and C ++. in this way, the database name must also be camped. To be discussed.

Variable:
Lowercase letters, separated by underscores (_), for example, $ user_id.
Different from Java and C ++. To be discussed.
Class member variables, function parameters, class instantiation into an object, all comply with the variable naming rules.
Cause: The URI and database have lower case conventions. the parameters obtained from $ _ GET and $ _ POST are stored in the database, so the values are in lower case.
PHP built-in variables $ _ GET and $ _ POST start with an underline, all uppercase. Do not start a custom variable with an underscore no matter how important it is to avoid conflicts with built-in variables in the future.
For example, do not use $ _ PUT or $ _ DELETE.

Constant:
All are uppercase and separated by underscores. Such as const MEMCACHE_TTL = 600;

PHP short labels:
Use , Do not use short labels . It conflicts with xml and is not conducive to deployment.

Class braces wrap:
You can use braces to separate one line, or put braces in one line with other lines. there is no final dispute to be discussed.
Class UserModel
{

}
Support for changers:
Http://www.php.net/manual/zh/language.oop5.basic.php
Http://pear.php.net/manual/en/standards.classdef.php

For more information, click the next page!
  • 2 pages in total:
  • Previous Page
  • 1
  • 2
  • Next page

Semantic 1. when you see the name in semantics, you will know the meaning. 2. the general prefix "is" indicates whether or not, "get" indicates read, and "set" indicates write. Is followed by adjectives rather than nouns, such...

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.