PHP file format

Source: Internet
Author: User
Tags case statement naming convention alphanumeric characters zend framework
PHP file format

For files containing only PHP code, the end flag ("?>") is not allowed, PHP itself does not need ("?>"), so that it can prevent the end of it is accidentally injected corresponding.

Important: The contents of any binary code allowed by __halt_compiler () are forbidden by PHP files in the Zend Framework or by files produced by them. The use of this feature is only available for some installation scripts.

Indentation is made up of four spaces, and Tab tab is forbidden.

Maximum length of a row

A line of 80 characters is more appropriate, that is, ZF developers should try to keep each line of code less than 80 characters, in some cases, the long point can be, but a maximum of 120 characters.

Line End Flag

Line end flags follow the conventions of Unix text files, which must end with a single line break (LF). NewLine characters are represented as 10, or 16-0x0A in a file.

Note: Do not use the carriage return (0x0D) of the Apple operating system or the return line combination of the Windows computer, such as (0x0d,0x0a).

Naming conventions

The Zend framework's class naming always corresponds to the directory structure of the file to which it belongs, the root directory of the ZF Standard library is "zend/", and the ZF Special (extras) library's root is "zendx/", and all Zend Framework classes are placed under it by rank.

The class name only allows alphanumeric characters and in most cases does not encourage the use of numbers. The underscore only allows path separators, for example, the corresponding class name in the zend/db/table.php file is zend_db_table.

If the class name contains multiple words, the first letter of each word must be capitalized, and successive caps are not allowed, for example "zend_pdf" is not allowed, and "zend_pdf" is acceptable.

These conventions define a pseudo-namespace mechanism for the Zend Framework. If the developer is practical in their program, the Zend Framework will take the PHP namespace feature (if any).

See examples of class names conventions in standard and special libraries. Important: Code that relies on the ZF library is not part of a standard or special library (such as program code or libraries that are not Zend published), and do not start with "Zend_" or "zendx_".

Filename

For other files, only alphanumeric characters, underscores, and dashes ("-") are available, and spaces are definitely not allowed.

Any file that contains any PHP code should end with a ". php" extension, except for well-known view scripts. The following examples give an acceptable file name for the Zend Framework class:

zend/db.phpzend/controller/front.phpzend/view/helper/formradio.php                

The file name must follow the rules for the corresponding class name above.

Functions and methods

The function name contains only alphanumeric characters and underscores are not allowed. Numbers are permissible but are discouraged in most cases.

The function name always begins with lowercase, and when the name of the letter contains multiple words, each child must have the first letter capitalized, which is called the "hump" format.

We generally encourage the use of lengthy names, which should be long enough to describe the intent and behavior of the function.

These are examples of acceptable function names:

Filterinput () getElementById () widgetfactory ()                

For object-oriented programming, accessors for instances or static variables are always prefixed with "get" or "set". In design pattern implementations, such as single-state mode (singleton) or Factory mode (factory), the name of the method should contain the name of the pattern, so that the name is more descriptive of the entire behavior.

In a method in an object declared as "private" or "protected", the first character of the name must be a single underscore, which is the only underscore used in the method name. The declaration is "public" and never contains an underscore.

Global functions (such as "floating functions") allow but are discouraged in most cases, it is recommended to encapsulate such functions in static classes.

Variables contain only alphanumeric characters, and in most cases, numbers are discouraged and underscores are not accepted.

An instance variable name declared as "private" or "protected" must begin with a single underscore, which is the only underline used in the program, and the Declaration as "public" should not begin with an underscore.

As with the function name (see section 3.3 above), the variable name always begins with a lowercase letter and follows the "camel-type" naming convention.

We generally encourage the use of lengthy names, which makes it easy to understand the code, and the developer knows where to save the data. Except in small loops, it is discouraged to use concise names such as "$i" and "$n". If a loop has more than 20 lines of code, the variable name of the index must have a descriptive name.

Constants contain numeric alphabetic characters and underscores, and numbers are allowed as constant names.

All letters of a constant name must be capitalized.

The words in a constant must be separated by an underscore, such as embed_suppress_embed_exception but not so embed_suppressembedexception.

Constants must be defined by "const" as members of the class, and global constants defined with "define" are strongly discouraged.

Coding style

PHP Code Partitioning (demarcation)

PHP code is always bound with the full standard PHP tag:

 
  

Short label () is not allowed, only files containing PHP code, do not end the tag (see General).

String

String literals

When a string is literal (without a variable), it should be enclosed in single quotation marks (apostrophe):

$a = ' Example String ';                    

string literal with single quotation mark (')

When a literal string contains single quotation marks (apostrophe), it is enclosed in double quotation marks, especially useful in SQL statements:

$sql = "Select ' id ', ' name ' from ' People ' WHERE ' name ' = ' Fred ' OR ' name ' = ' Susan '";                    

The above syntax is preferred when escaping single quotes because it is easy to read.

Variable substitution

Variable substitution has the following forms:

$greeting = "Hello $name, welcome back!"; $greeting = "Hello {$name}, welcome back!";                    

In order to remain consistent, this form does not allow:

$greeting = "Hello ${name}, welcome back!";                    

String connection

The string must be concatenated with the "." operator to increase readability by adding a grid around it:

$company = ' Zend '. ' ' . ' Technologies ';                    

When using the "." Operator connection string, the code can be encouraged to be divided into multiple lines, also to improve readability. In these examples, each successive line should be filled by whitespace, such as "." and "=" Alignment:

$sql = "Select ' id ', ' name ' from ' People '"     . WHERE ' name ' = ' Susan ' "     ." ORDER by ' name ' ASC;                    

Numeric index Array

Index cannot be a negative number

The recommended array index starts at 0.

When you declare an indexed array with the array function, spaces are spaced after each comma to improve readability:

$sampleArray = Array (1, 2, 3, ' Zend ', ' Studio ');                    

You can declare multiple rows of indexed arrays with "array", and fill the lines with spaces at the beginning of each successive line:

$sampleArray = Array (1, 2, 3, ' Zend ', ' Studio ',                     $a, $b, $c,                     56.44, $d, 500);                    

Associative arrays

When associating an array with a declaration, array we encourage dividing the code into multiple lines, filling the alignment and values with spaces at the beginning of each successive line:

$sampleArray = Array (' firstkey ' = '  firstvalue ',                     ' secondkey ' = ' secondvalue ');                    

Declaration of the class

Use the naming convention of the Zend Framework to name the class.

Curly braces should start with a line from the class name (the "One True brace" form).

Each class must have a document block that conforms to the Phpdocumentor standard.

All code in a class must be indented with four spaces.

There is only one class in each PHP file.

Placing additional code in the class is allowed but discouraged. In such a file, two lines of space are used to separate classes and other code.

The following is an example of an acceptable class://459 9506-441 9658 next time, start here.

/** * Documentation Block here */class sampleclass{//    class All contents    //Must indent four spaces}                    

Class member variable

Class member variables must be named with the variable name conventions of the Zend Framework.

The declaration of a variable must be declared at the top of the class, above the method.

var is not allowed (because ZF is based on PHP 5), use private, protected, or public. Direct access to public variables is allowed but discouraged, preferably using accessors (set/get).

Functions and methods

function and Method declarations

Functions must be named with the function name conventions of the Zend Framework.

Functions in a class must declare their visibility with private, protected, or public.

Like classes, curly braces start with the next line of the function name (the "One True brace" form).

There are no spaces between the function name and the parentheses enclosing the parameter.

Strongly oppose the use of global functions.

The following is an example of an acceptable function declaration in a class:

/** * Documentation block here */class foo{    /**     * documentation Block HERE     * * Public    function bar ()    {        //All contents of function        //must indent four spaces    }}                    

Note: A pass-through (pass-by-reference) is the only parameter-passing mechanism allowed in a method declaration.

/** * Documentation block here */class foo{    /**     * documentation Block HERE     * * Public    function bar (& ; $baz)    {}}                    

The address is strictly forbidden at the time of invocation.

The return value cannot be in parentheses, which hinders readability and if future methods are modified to address, the code is problematic.

/** * Documentation Block here */class foo{    /**     * Wrong * * Public    function bar ()    {        return ($ This->bar);    }    /**     * Right     *    /Public Function bar ()    {        return $this->bar;    }}                    

Use of functions and methods

The parameters of the function should be separated by commas and the following spaces, and the function in the example of an acceptable invocation can have three parameters:

Threearguments (1, 2, 3);                    

The address method is strictly forbidden at the time of invocation, see the Declaration section of the function to correctly use the function's address method.

Functions with array parameters, function calls can include "array" hints and can be divided into multiple lines to improve readability, while the standard for writing arrays still applies:

Threearguments (Array (1, 2, 3), 2, 3); threearguments (Array (1, 2, 3, ' Zend ', ' Studio ',                     $a, $b, $c,                     56.44, $d, 500), 2, 3);                    

Control statements

If/else/elseif

Control statements that use if and ElseIf must have a space before and after the parentheses of the conditional statement.

In parentheses, the conditional statements, operators must be separated by spaces, encourage the use of multiple parentheses to improve the logical combination in complex conditions.

The former curly braces must be on the same line as the conditional statement, and the curly braces are in the last line alone, with the contents indented with four spaces.

if ($a! = 2) {    $a = 2;}                    

For "If" statements that include "ElseIf" or "else", and the format of the "If" structure, the following example example "If" statement, including the format conventions for "ElseIf" or "Else":

if ($a! = 2) {    $a = 2;} else {    $a = 7;} if ($a! = 2) {    $a = 2;} elseif ($a = = 3) {    $a = 4;} else {    $a = 7;}                    

In some cases, PHP allows these statements to be used without curly braces, but in the (ZF) code standard, they ("If", "ElseIf" or "else" statements) must use curly braces.

"ElseIf" is permissible but strongly discouraged, and we support the "else if" combination.

Switch

Control statements in the "switch" structure must have a single space around the parentheses of the conditional statement.

The code in "switch" must have four spaces indented, and the code in "case" will then indent four spaces.

Switch ($numPeople) {case    1: Break        ;    Case 2: Break        ;    Default: Break        ;}                

The switch statement should have default.

Note: Sometimes it is useful to not write a break or return in a case statement that falls through to the next. In order to distinguish it from a bug, any case statement that does not write a break or return should have a comment such as "//Break intentionally omitted" to indicate that the break was deliberately ignored.

Comment Document

All document blocks ("Docblocks") must be compatible with the Phpdocumentor format, and the description of the Phpdocumentor format is beyond the scope of this document, for more information about it, refer to:»http://phpdoc.org/.

All class files must contain the file-level ("File-level") DocBlock at the top of the file, placing a "class-level" DocBlock at the top of each class. Here are some examples:

Each file containing PHP code must contain these phpdocumentor tags at least docblock at the top of the file:

/** * A short description of the file * * The detailed description of the file (if any) ... * LICENSE: some LICENSE information * * @copyright  Copyright (c) 2005-2011 Zend Technol Ogies USA Inc. (http://www.zend.com) * @license    http://framework.zend.com/license/3_0.txt   BSD License * @ Version    $Id: $ * @link       http://framework.zend.com/package/PackageName * @since      File available since Release 1.5.0*/                    

Each class must contain at least these phpdocumentor tags:

Description of the/** * class (if any) ... * * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (Http://www.zen d.com) * @license    http://framework.zend.com/license/   BSD license * @version    Release: @package_version @ * @ Link       http://framework.zend.com/package/PackageName * @since      Class available since Release 1.5.0 * @deprecated Class deprecated in Release 2.0.0 */                    

Each function, including the object method, must have a document block (DocBlock) that contains at least the following:

Description of the function

All parameters

All possible return values

Because the access level has been declared with "public", "private", or "protected", you do not need to use "@access".

If the function/method throws an exception, use @throws to all known exception classes:

@throws Exceptionclass [Description]                    

Source: http://framework.zend.com/manual/zh/coding-standard.php-file-formatting.html

  • 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.