PHP coding standard for Zend Framework

Source: Internet
Author: User
Tags functions sql naming convention variables php file variable zend zend framework

[Excerpt from Zend Framework Official document]

C.2. PHP file format
c.2.1. General
For files that contain only PHP code, the end sign ("?>") is not allowed, and PHP itself does not need ("?>") to prevent its end from being accidentally injected accordingly.
Important: The contents of any binary code allowed by __halt_compiler () are prohibited by PHP files in the Zend Framework or by the files they generate. The use of this feature is only open to some setup scripts.

c.2.2. Indentation
Indentation consists of four spaces and prevents tab tabs from being used.

c.2.3. Maximum length of line
A line of 80 characters is appropriate, that is, the ZF developer should try to keep every line of code less than 80 characters if possible, and in some cases, a long point, but a maximum of 120 characters.

c.2.4. Line End Flag
The line end flag follows the convention of the Unix text file, and the line must end with a single line break (LF). Line breaks are represented in the file as 10, or 16 0x0A.
Note: Do not use the carriage return (0x0D) of the Apple operating system or the return line-wrapping combination of Windows computer (0x0d,0x0a).

C.3. Naming convention
c.3.1. Class
The Zend framework's class naming always corresponds to the directory structure of the files it belongs to, and the ZF standard library's root directory is "zend/", and the ZF Special (extras) library's root directory is "zendx/", where all Zend Framework classes are stored hierarchically.
The class name allows only alphanumeric characters and, in most cases, does not encourage the use of numbers. Underscores allow only the path delimiter; for example, the corresponding class name in the zend/db/table.php file is zend_db_table.
If the class name contains more than one word, the first letter of each word must be capitalized, and consecutive 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 use the PHP namespace feature (if any).
See examples of class names in standard and special libraries as class name conventions. Important: Code that relies on the ZF library to expand, but is not part of a standard or special library (such as program code or a library that is not a Zend issue), does not begin with "Zend_" or "zendx_".

c.3.2. File name
For other files, only alphanumeric characters, underscores, and dashes ("-") are available, and spaces are absolutely not allowed.
Any file that contains any PHP code should end with the ". PHP" extension, except for well-known view scripts. The following examples give an acceptable filename for the Zend Framework class:
zend/db.php
zend/controller/front.php
zend/view/helper/formradio.php
The file name must follow the rules for the corresponding class name above.

c.3.3. Functions and methods
The function name contains only alphanumeric characters, and underscores are not allowed. Numbers are allowed but are discouraged in most cases.
Function names always start with lowercase, and when the functions name contains multiple words, each child's first letter must be capitalized, which is called the "hump" format.
We generally encourage the use of lengthy names, which should be long enough to explain the intent and behavior of the function.
These are examples of acceptable function names:
Filterinput ()
getElementById ()
Widgetfactory ()

For object-oriented programming, accessors for instance or static variables are always prefixed with "get" or "set". In the implementation of design patterns, such as a single State pattern (singleton) or a factory pattern (factory), the method's name should contain the name of the schema, so that the name is more descriptive of the entire behavior.
In an object, the first character of the name must be a single underline, which is the only underline used in the method name, declared as "private" or "protected". Never include an underscore declared as "public."
Global functions (such as "floating functions") allow, but are not encouraged in most cases, the proposed encapsulation of such functions into static classes.

c.3.4. Variable
Variables contain only alphanumeric characters, and in most cases the use of numbers is discouraged and underscores are not accepted.
Instance variable names declared as "private" or "protected" must begin with a single underscore, which is the only underline used in the program and should not begin with an underscore declared as "public."
As with the function name (see section 3.3 above), variable names always start with a lowercase letter and follow the "hump" naming convention.
We generally encourage the use of lengthy names, so that the code is easy to understand, and developers know where to store the data. The use of concise names such as "$i" and "$n" is discouraged unless it is in a small cycle. If a loop exceeds 20 lines of code, the variable name of the index must have a descriptive name.

c.3.5. constant
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 delimited by an underscore, such as embed_suppress_embed_exception but not so embed_suppressembedexception.
Constants must be defined by "const" as members of a class and strongly discourage the use of global constants defined by "define".

C.4. Coding style
c.4.1. PHP code Partitioning (demarcation)
PHP code is always bounded with the full standard PHP Tags:
<?php

?>
The short label () is not allowed, contains only the PHP code file, do not end the tag (see section c.2.1, "General").

c.4.2. String
c.4.2.1. String literals
When the string is literal (does not contain a variable), it should be enclosed in single quotes (apostrophe):
$a = ' Example String ';

c.4.2.2. String literals that contain single quotes (')
When a literal string contains single quotes (apostrophe), it is enclosed in double quotes, particularly 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.

c.4.2.3. Variable substitution
Variable substitution has the following forms:
$greeting = "Hello $name, welcome back!";
$greeting = "Hello {$name}, welcome back!";
To remain consistent, this form does not allow:
$greeting = "Hello ${name}, welcome back!";

c.4.2.4. String concatenation
The string must be connected with the "." operator to increase readability by adding a space above it:
$company = ' Zend '. ' ' . ' Technologies ';
When using the "." operator to connect strings, encouraging the code to be divided into multiple rows is also to improve readability. In these examples, each successive row should be filled by whitespace, such as "." and "=" Alignment:
$sql = "Select ' id ', ' name ' from ' People '"
. "WHERE ' name ' = ' Susan '"
. "ORDER BY ' name ' ASC";

c.4.3. Array
c.4.3.1. Array of numeric indices
Index cannot be negative, it is recommended that the array index start at 0.
When you declare an indexed array with the array function, space is spaced at the back of each comma to improve readability:
$sampleArray = Array (1, 2, 3, ' Zend ', ' Studio ');

You can declare an array with multiple rows indexed by using "array", and fill the alignment with spaces at the beginning of each contiguous line:
$sampleArray = Array (1, 2, 3, ' Zend ', ' Studio ',
$a, $b, $c,
56.44, $d, 500);

c.4.3.2. Associative array
When using a declaration associative array, we encourage the code to be divided into multiple lines, filled with spaces at the beginning of each contiguous line to alignment and values:
$sampleArray = Array (' Firstkey ' => ' firstvalue '),
' Secondkey ' => ' secondvalue ');
c.4.4. Class
Declaration of c.4.4.1. Class
Use the naming convention of the Zend Framework to name the class.
The curly braces should start with a line on 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.
Put additional code into the class to allow but not encourage. In such a file, two lines of space are used to separate classes and other code.

Here is an example of an acceptable class://459 9506-441 9658 The next time you start here.
/**
* Documentation Block
*/
Class SampleClass
{
All contents of the class
Must indent four spaces
}

c.4.4.2. class member variable
Class member variables must be named with the Zend Framework's variable name conventions.
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) and is used private, protected, or public. Direct access to public variables is allowed but discouraged, preferably using accessors (set/get).

c.4.5. Functions and methods
c.4.5.1. Functions and Method declarations
You must use the function name convention of the Zend Framework to name the function.
Functions in a class must declare their visibility with private, protected, or public.
Like a class, curly braces begin with the next line of the function name (the "One True brace" form).
There are no spaces in the middle of a function name and parentheses.
Strongly opposed to using global functions.
The following is an example of an acceptable function declaration in a class:
/**
* Documentation Block
*/
Class Foo
{
/**
* Documentation Block
*/
Public Function Bar ()
{
All contents of the function
Must indent four spaces
}
}
Note: The address (pass-by-reference) is the only parameter passing mechanism allowed in the method declaration.

/**
* Documentation Block
*/
Class Foo
{
/**
* Documentation Block
*/
Public Function bar (& $baz)
{}
}

The address is strictly prohibited when invoked.
The return value cannot be in parentheses, which hinders readability and if future methods are modified to address the code, there is a problem.

/**
* Documentation Block
*/
Class Foo
{
/**
* Wrong
*/
Public Function Bar ()
{
Return ($this->bar);
}

/**
* Right
*/
Public Function Bar ()
{
return $this->bar;
}
}

c.4.5.2. 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 method of passing is strictly prohibited at the time of the call, see the Function Declaration section how to correctly use the method of how the function is passed.
Functions with array arguments, which can include "array" hints and can be split 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);

c.4.6. Control statements
c.4.6.1. If/else/elseif
Control statements that use the IF and ElseIf must have a space before and after the parentheses of a conditional statement.
The conditional statements in parentheses, which must be separated by spaces, encourage the use of multiple parentheses to increase the logical combination in complex conditions.
The curly braces must be on the same line as the conditional statement, with the following curly braces on the last line, with the contents indented in four spaces.
if ($a!= 2) {
$a = 2;
}

The "If" statement, which includes "ElseIf" or "else", is similar to the format of the "if" structure, as in the following example "if" statement, which includes the format convention 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 free of curly braces, but in the (ZF) code standards, they ("If", "ElseIf" or "else" statements) must use curly braces.
"ElseIf" is allowed but strongly discouraged, and we support "else if" combinations.

c.4.6.2. Switch
Control statements in the switch structure must have a single space before and after the parentheses in the conditional statement.
The code in "switch" must have four spaces indented, and the code in "case" is indented 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 differentiate from a bug, in any case statement, there should be a "//break intentionally omitted" annotation to indicate that the break was deliberately ignored where no break or return is written.

c.4.7. Note Document
c.4.7.1. Format
All document blocks ("Docblocks") must be compatible with the Phpdocumentor format, and Phpdocumentor format descriptions are beyond the scope of this document, and for details on 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:

c.4.7.2. File
Each file that contains PHP code must contain these phpdocumentor tags at least docblock at the top of the file:
/**
* Brief description of the file
*
* Detailed description of the file (if any) ...
*
* LICENSE: Some LICENSE information
*
* @copyright Copyright (c) 2005-2010 Zend Technologies 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
*/

c.4.7.3. Class
Each class must contain at least these phpdocumentor tags:
/**
* Description of the class
*
* Detailed description of the class (if any) ...
*
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.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
*/

c.4.7.4. function
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 through "public", "private", or "protected", you do not need to use "@access".
If a function/method throws an exception, it uses @throws to all known exception classes:
@throws Exceptionclass [Description]



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.