PHP writing specifications (1)

Source: Internet
Author: User

PHP writing specifications

Author: sink

Last modified: 2011-7-7

References:

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. 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 will be more and more things in them and they will 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:

 
 
  1. class BlogModel  
  2. {  
  3.    public function addBlog()  
  4.     {  
  5.     }    
  6.     public function updateBlog()  
  7.     {  
  8.     }  

File notes:

Note followed by <? Next line in php. Indicate the author. @ Version does not need to be written at the moment, because svn provides version management.

The format follows the requirements of PHPdoc:

Http://manual.phpdoc.org/HTMLframesConverter/default/phpDocumentor/tutorial_tags.author.pkg.html

 
 
  1. <? Php
  2. /**
  3. * Various blog services: Add and update
  4. * @ Author sink
  5. *
  6. */
  7. Class BlogModel
  8. {
  9.  
  10. }
  11. ?>

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.

1

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.