Zendframework, PHP encoding Specification

Source: Internet
Author: User

I remember writing a PHP code specification before.ArticleThe format here is the ZF specification, most of which can be used in PHP for non-ZF projects.

To be honestProgramIf it is correct, I partially do not comply with it.

PHP File Format

1.?>

?> If it is the end, it is not required in PHP. What is the purpose of not writing?> When a space is displayed, the file has output. An error such as session already send may occur when output is available. Try to make it?> No output at the end. With ZF,CodeHybrid coding with pages has been a thing of the last century, so what if your program has?> If something else appears later, it seems that we have to continue to chew on the manual and take view and controller as the preview.

2. indent

Four spaces. This has become the software industry standard.

3. Maximum line length

We recommend that each line contain a maximum of 80 characters and a maximum of 120 characters. When you exit the screen, you still need to pull the horizontal scroll bar, so try to add more lines. Habits: Some people use a wide screen, which can contain up to 160 characters.

4. linefeed

We recommend that you use Unix like line breaks instead of line breaks for Mac and windows.

\ N is used, \ r or \ r \ n is not used

What is the need for saving a symbol? Or is Php mainly running on UNIX like? Well

Naming Method

1. Class

ZF class names are as follows:

I am not quite clear about this. If you can read the manual, try to read it.

Let me give you a few examples to explain the following:

ZF classes are written in the Zend/directory.

The class in the file Zend/DB/table. php is zend_db_table.

Similarly, the class in the file Zend/feed/RSS. php is zend_feed_rss.

Note that the class name and file path match the file name, and the first letter of each word must be capitalized.

Note: Do not start classes written by yourself with Zend.

2. Interface

This is basically the same as the class

A little more, that is, it must end with _ interface. Therefore, if the file name is interface, it is the interface.

Example: zend_controller_dispatcher_interface

3. File Name

In addition to the above types, other file names can start with a letter or underscore (-). Do not use spaces.

All PHP code files must end with. php.

Example: Zend/controller/front. php

Example: Zend/View/helper/formradio. php

4. Functions and Methods

A function can only start with a letter.

ZF recommends that all functions be placed in the class.

The function name must start with a lowercase letter. If it is composed of multiple words, the remaining words must be uppercase letters. (Camelcaps)

Example: getdate (), getelementbyid ()

In object orientation, set/get is required for access to member variables. This design mode is mentioned.

Example: Private $ _ myz; Public Function setmyz () {}; public function getmyz (){};

5. Variables

A variable can only start with a letter.

Variables of the private and protected types must begin with an underscore.

Public variables cannot start with an underscore.

The variable name must start with a lowercase letter. If it is composed of multiple words, the remaining words must be uppercase letters. (Camelcaps)

6. Constants

A constant must start with an uppercase letter and consist of uppercase letters and underscores. Without the $ symbol.

Example: php_self

Constants should be defined in the class: const myz_self

Encoding Style

1. <? PHP?>

Short labels are not recommended in ZF: <?>. I do not recommend it either...

Another point is: do not end with?>, The reason is explained above.

2. String

If the string is a pure character (without variables), it is enclosed in single quotes.

$ Myz = 'myz is Cowboy! ';

If quotation marks are required in a string, double quotation marks are used. single quotation marks are used in SQL statements.

$ Myz = "select * From myfavorites where name = 'framework' limit 0, 1 ";

The string contains variables. Double quotation marks are used.

$ Myz = "$ name is Cowboy! "; $ Myz =" {$ name} is Cowboy! ";

This is wrong: $ myz = "$ {name} is Cowboy! "; (Although this is correct in PHP ).

String connection

$ ZF = 'zend'. 'framework ';

To wrap a line, write in the following format:

$ ZF = 'zend'

. 'Framework'

. 'Is'

. 'Good ';

3. Array

Number index. The index starts from 0.

$ Myz = array (1, 2, 3, 'myz', 'great', $ );

String index. The index name is customized.

$ Myz = array ('zend' => '1. 0.1 ',

'Php' => '5. 1.2 ',

'Apache' => '2. 2.4 ');

4. Class

4. 1. Class Declaration

Each half of the {} extension number after the class must be a single row.

Each class requires comments that comply with the phpdocumentor standard.

All codes in the class must be indented by four characters.

A PHP file can contain only one class.

Try to avoid writing other code to a PHP file. If there is one, leave two lines blank.

For example:

Require_once 'zend/pdf/resource/image/JPEG. php ';

/**

* Documentation block here

*/

Class sampleclass

{

// Entire content of class

// Must be indented four spaces

}

. Member variables of the class

The member variables must be defined in the class prior to the function.

Do not use VaR to define variables. use private, protected, and public. do not define member variables as public, but access them using set/get.

5. Functions and Methods

5. 1. Declaration of functions and methods

Class functions must use private, protected, and public.

The format is the same as that of the class. You also need to exclusive one row of the Large Number extension ticket.

() After the function is followed by the function name.

/**

* Documentation block here

*/

Class foo

{

/**

* Documentation block here

*/

Public Function bar ()

{

// Entire content of Function

// Must be indented four spaces

}

}

The parameter after the function can also use the reference variable public function bar (& $ bar), which must be defined as & before use.

Return ($ this-> bar); // This is incorrect. To be written as: return $ this-> bar; To improve readability.

. Use of functions and methods

If there are multiple function parameters, they must be separated by commas (,) and followed by spaces.

Bar ($ A, $ B, $ C );

6. control statements

6.1.if/else/elseif

If, else, And elseif all occupy one row.

In the condition, there must be spaces before and after the operator to improve readability.

The starting braces are the same as if, and the ending braces start with another line.

For example:

If ($! = 2 ){

$ A = 2;

} Elseif ($ A = 3 ){

$ A = 4;

} Else {

$ A = 7;

}

Use elseif as much as possible instead of writing another if.

You may not use {} at some time {}. For example, there is only one row.

6.2.switch

Similar to if, the braces start with switch, and the braces end with a new line.

Each case requires four spaces for indentation.

Switch ($ numpeople ){

Case 1:

Break;

Case 2:

Break;

Default:

Break;

}

Each case requires a break. To disable break for some special purpose, you must specify: // break intentionally omitted.

7. Notes

. All comments must comply with the phpdocumentor specification. Http://phpdoc.org/

For specific specifications, see the content in the ZF file.

. File comments

Each file must be annotated at the beginning and comply with the phpdocumentor standard.

/**

* Short Description for file

*

* Long description for file (if any )...

*

* License: Some license information

*

* @ Copyright 2005 Zend Technologies

* @ License http://www.zend.com/license/3_0.txt PHP license 3.0

* @ Version CVS: $ ID: $

* @ Link http://dev.zend.com/package/PackageName

* @ Since file available since release 1.2.0

*/

. Class Annotation

/**

* Short Description for class

*

* Long description for class (if any )...

*

* @ Copyright 2005 Zend Technologies

* @ License http://www.zend.com/license/3_0.txt PHP license 3.0

* @ Version release: @ package_version @

* @ Link http://dev.zend.com/package/PackageName

* @ Since class available since release 1.2.0

* @ Deprecated class deprecated in release 2.0.0

*/

7.4 function comments

The description, all parameters, and return values must be specified in the function annotation.

If an exception is thrown, @ throws exceptionclass [description] is required.

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.