Best 11 PHP programming specifications and 11PHP programming specifications

Source: Internet
Author: User

Best 11 PHP programming specifications and 11PHP programming specifications

Personal Original Web site: http://www.phpthinking.com/archives/375

PHP has been widely used to develop Web-based applications since its design. Because PHP is a scripting language, some rules must be followed during development.

This article will discuss common good code habits, or code specifications, in the PHP field.
1. Enable Error Report

Error Reporting is a very useful feature in PHP and should be enabled simultaneously in the development phase. This helps us determine the problems in our code. The most common feature is "E_ALL", which helps us identify all warnings and serious errors. It must be noted that we should disable this function before putting our code into service. Otherwise, all potential errors and warnings will be exposed in the browser.

2. Use the DRY Principle

'Do not Repeat Yourself '. The DRY principle means not to Repeat your code .. This concept is a very useful programming concept that can be hard applied in any programming language, such as Java, C #, or PHP. Use the DRY principle to ensure that there is no redundant code in the program.
A solution that violates the DRY principle is usually called WET, which refers to "write everything twice". The code that we write with the same function will appear more than once.
Let's see the following code:
Listing1:
WET code method:

1 $mysql = mysql_connect ( 'localhost''mysqladmin_uid''mysqladmin_pwd');
2 mysql_select_db( 'DB_NAME' or die"Sorry !! No database selected!");

The following uses the DRY principle to optimize the Code:

1 $db_host ' localhost ';
2 $db_user ' mysqladmin_uid ';
3 $db_password ' mysqladmin_pwd ';
4 $db_database ' DB_NAME ';
5 $mysql = mysql_connect($db_host$db_user$db_password);
6 mysql_select_db($db_database);

3. Use indentation and spaces as appropriate.

When writing code in any programming language, make sure that the code is provided where necessary with appropriate indentation and spaces. To increase the readability of the code and manage the code in a more effective way.

4. Use meaningful and consistent naming standards

As with any programming language, PHP veterans also recommend that we follow meaningful naming standards. We have two main ways to achieve this:
A. camper
In this method, the first letter is lowercase, and the first letter of each word is followed by uppercase.
Listing2:

1 public class MyClass {
2 public void methodName(String argName) {
3 }
4 }

B. Underline
In this method, we underline every two words ("_"). When using this method, the code can be modified as follows:
Listing3: Use underline code snippets

1 public class MyClass {
2 public void method_name(String arg_name) {
3 }
4 }

5. Avoid deep nesting

Multi-level nesting reduces code readability in any development language. Any developer should avoid using deep nesting.
Listing4: code snippets with multi-level nesting

01 <?php
02 class MyClass {
03 public function method_name($arg_name) {
04 if (is_writable $folder )) {
05 if ($fp fopen $file_location_path'w' )) {
06 if ($stuff = extractSomeConditionalStuff ()) {
07 if (fwrite ( $fp$stuff )) {
08 // ...
09 else {
10 return false;
11 }
12 else {
13 return false;
14 }
15 else {
16 return false;
17 }
18 else {
19 return false;
20 }
21 }
22 }
23 ?>

The above code is a simple nested code. We can see that it is very difficult if the block ends. For better readability, let's modify the code:
Listing5: code snippets to avoid multi-level nesting

01 function method_name (String arg_name) {
02 // ...
03 if (! is_writable $folder )) {
04 return false;
05 }
06 if (! $fp fopen $file_location_path'w' )) {
07 return false;
08 }
09 if (! $stuff = extractSomeConditionalStuff ()) {
10 return false;
11 }
12 if (fwrite ( $fp$stuff )) {
13 // ...
14 else {
15 return false;
16 }
17 }

6. Add the expected comments.

During development, make sure that your source code has enough line comments. This is a standard practice that should be followed. This helps to analyze the code in depth, because the person in charge of the encoding will not always remain unchanged. Even if the same person is asked to make some simple changes in the Code, comments in the line will always help to understand what code is used for at the time. To use better annotation standards in PHP, we recommend that you check some standard PHP document packages, such as phpjavasentor.

7. Do not put the PHPINFO () function in the root directory of the website.

The phpinfo () function is a very important function and should be used with caution. With this function, anyone can view the details of the server environment. It is best to always put
Use this code in a secure file.

8. Never trust users

If your application involves any user input, you must write secure code to handle it in this way because it contains various possible inputs. To avoid program injection attacks or damage data integrity, you must verify the data filtering format. You can read this article "terrible code injection methods"

9. make rational use of the cache mechanism

Good programming methods are always recommended to use the cache mechanism cache to help us achieve better performance.

In the PHP world, cache is used for implementation:
Memcached-a key-Value Pair stored in small data block storage.
APC -- Optional PHP cache for open PHP operation code Cache
XCache-a fast and reliable PHP operation code Cache
Zend Cache-API is a set of advanced high-speed Cache functions.
EAcclerator-open-source cache Tool

10. Avoid copying existing variables.

Copying a predefined variable to a local variable with a small name is not a good programming habit. This adversely affects the performance of applications. Let's take a look at the following code snippet:
Listing6: copy existing Variables

1 $desc strip_tags($_POST['PHP description']);
2 echo $desc;

The code above is an example of copying a variable as a local variable that is not necessary. Is this a good practice. To achieve the same effect, you can use the following code:

1 echo strip_tags($_POST['PHP description']);

11. Use the framework

Frameworks are developed after a large amount of research, so they prove to be less problematic. They make development easier because they provide mature solutions. There are many available frameworks in PHP. You should use this in the development process. One of the frameworks is widely used in MVC or model view controllers.

Conclusion:

Programming specifications guide us to develop code more effectively.
Follow programming specifications to ensure better application performance.
As in other programming languages, to create high-quality code, PHP also needs to follow this good programming specification.


Php programming specifications

There are many rules, so we have found a better one for you. Similar to other versions.
Reference: wenku.baidu.com/..f.html
 
Php object-oriented programming code specification

Class Name
A) uppercase letters are used to separate words. Other letters are in lower case, that is, the camper format.
B. Use uppercase letters for the first letter of the name.
C. Do not use underscores ('_')
D) it is best to use the upper-case letter I for the interface Interface interface and end with the Interface
For example:
Class NameOneTwo
Class Name
Interface IExampleInterface ()
Method Name
A) Use uppercase letters to separate words, and all other letters Use lowercase letters.
B) The first letter of the name should be in upper case. If it is declared as "private" or "protected", it should be prefixed '_'.
C. Do not use underscores ('_')
D) (rules consistent with the class name)
E) The object accessors are always prefixed with "get" or "set". When using design patterns such as singleton)
Class attribute name
A) The attribute name prefix should be specified by the attribute value type (For details, refer to the variable naming Rules)
B) use the same naming rules as the class after the prefix.
C) Private attributes are prefixed '_'.
For example:
Class NameOneTwo {
Public function VarAbc (){};
Public function ErrorNumber (){};
Public $ iAge;
Private $ _ iAge;
}
Global Variables
A) The global variable must have the prefix 'G'
B) Other naming rules for reference Variables
For example:
Global $ gi_Age;
Global $ ga_Price

Related Article

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.