PHP Programming Code Specification Learning notes (for Beginners) (1/2)

Source: Internet
Author: User
Tags lowercase naming convention php code php programming

Naming conventions

The Theta class file is named after the. class.php, which is called with the Hump method, and the first letter is capitalized, for example Pay.class.php;
The Theta class name and directory _ file name are consistent. For example: The Directory of class name Zend_autoloader is zend/autoloader.class.php;
The name of the theta function uses lowercase letters and underscores. For example: GET_CLIENT_IP;
The Theta method is named using the Hump method, the first letter lowercase or using an underscore "_", such as Listcomment (), _getresource (), usually the method at the beginning of the underscore belongs to the private method;
The name of the theta attribute uses the Hump method, the first letter lowercase or the underscore "_", such as $username,$_instance, where the attribute at the beginning of the underscore is a private property;
Theta constants are named with uppercase letters and underscore "_", such as "Home_url";

Common nouns

1>list nouns (singular), such as listapple, we know that reading the Apple list, we do not need to write getapples or listapples or readapples--because get we specify generally used to read a single data, If Getapple.listapples does not add s We also know is to take the Apple list (guaranteed to shorten the variable naming);
2>get nouns (singular);
3> a noun total that represents the sum of something. such as Expensetotal;
4>found: Indicates whether a value has been found;
5>uccess or OK: an operation is successful;
6>done: Whether a project is completed;
7>error: Whether there is a mistake occurred;
8>result: Returned results

Code refactoring

1. The code within the function or method is controlled in one screen as much as possible.
2. Methods not used in the class are randomly deleted.
3. Modify the methods in other people's classes to sign.
4. Write a Readme file in each module (a description or code description for the more complex business).
5. Try to make each class do their own thing, each function to do one thing.

Here are some ways to add

First, file format

1. For files that contain only PHP code, we will ignore the "?>" at the end of the file. This is to prevent extraneous spaces or other characters from affecting the code.
For example:

The code is as follows Copy Code
<?php
$foo = ' Foo ';
2. Indentation should be able to reflect the logical results of the code, use four spaces as much as possible, and prevent tab tabs from being used because it guarantees flexibility across client programmer software.
For example:
if (1 = $x) {
$indented _code = 1;
if (1 = = $new _line) {
$more _indented_code = 1;
}
}

3. Variable assignment must remain equal spacing and arrangement.
For example:

The code is as follows Copy Code
$variable = ' demo ';
$var = ' Demo2 ';

4. The length of each line of code should be controlled within 80 characters and no longer than 120 characters. Because Linux reads in files generally in 80 units, that is, if a line of code more than 80 characters, then the system will pay extra operation instructions. This may seem like a minor problem, but it's also a standard to be aware of and follow for the perfect programmer.
5. No extra spaces are allowed at the end of each line.

Second, the naming convention

1. class files are suffixes of ". class.php" and the class file name allows only letters, named with the Hump method, and the first letter is capitalized, for example: DbMysql.class.php.
2. Files other than other class library files, such as configuration and functions, typically have a suffix of ". inc.php" and ". php", and the file name is named using lowercase letters and underscores, separated by an underscore between words, such as config.inc.php, common.php,install_function.php.
3. Ensure that file naming and invocation are consistent, due to case sensitivity on Unix-like systems.
4. The class name and file name are consistent (including the case above), and the class name only allows letters, such as the Useraction class's file name is UserAction.class.php, The filename of the Infomodel class is InfoModel.class.php.
5. The controller class has a suffix of action, such as Useraction, Infoaction, model class with the suffix, such as Usermodel, Infomodel, and other classes are also categorized as suffixes, such as service, Widgets.
6. Method names are only allowed by letters, underscores are not allowed, the first letter to lowercase, then each word first letter to capitalize, that is, the so-called "hump law naming" rule, and the more detailed the better, should be able to describe the function of the method, such as Switchmodel, Findpage.
7. The name of the attribute is only allowed by the letter, the underscore is not allowed, the first letter to lowercase, after each word to capitalize the first letter, that is, the so-called "hump law naming" rules, such as Tableprefix, TableName.
8. For access to object members, we must always use the "get" and "set" methods. For example,

The code is as follows Copy Code

Class Foo
{
protected $_testobj;
Public Function Gettestobj ()

{
return $this->_testobj;
}
Public Function Settestobj ($TESTOBJ)
{
$this->testobj = $_testobj;
}
}

Home 1 2 last page

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.