Home Links
Home Page
Use XML in PHP
Compression of the data on PHP
Use mod_perl
Style of coding on PHP
Perl and XML. Library of the programmer
Access to databases under management SUBD POSTGRES95
Parsing on Perl
XMLHttpRequest (AJAX) - sending and processing of answers of http-searches with help JavaScript.
Subsys_JsHttpRequest: pumping of the data without perezagruzki pages (AJAX)
The brief description of regular expressions: POSIX and PCRE
Optimization of searches in MySQL
Wound of treelike structures in Databases (Nested Sets)
Oracle / PHP FAQ
The specification and functions DOM in PHP
Not kehshirovat`!
Report PPP
Useful advice{councils} on optimization of ASP-applications
XML: time has come
 

Style of coding on PHP

A.4.1. A frame of a PHP-code


The PHP-code should always obramljatsja full PHP-tegami:



<? php

?>


Short tegi are not allowable.



A.4.2. Lines


A.4.2.1. Line literals


When the line is literal (does not contain substitutions of variables), for its{her} frame apostrophes or " unary inverted commas " should be used:



$a = ' Example String ';


A.4.2.2. The line literals containing apostrophes


When the line of literals itself contains apostrophes, it is authorized to use " double inverted commas " for a frame of a line. It is especially actual for SQL-searches:



$sql = " SELECT 'id', 'name' from 'people' WHERE 'name' = 'Fred' OR 'name' = 'Susan' ";

Syntax is more preferable, than shielding of apostrophes above.


A.4.2.3. Substitution of variables


Substitution of variables is authorized with use of two mentioned below forms:



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


For reliability, this form is not allowable:



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


A.4.2.4. Konkatenacija lines


Lines should will be united with the help of the operator ".". The blank should always dobavljatsja before and after the operator "." For improvement chitabel`nosti:



$company = 'Zend.' 'Technologies';


When it is made konkatenacija lines with the help of the operator ".", it is authorized to break off expression for some lines for improvement chitabel`nosti. In this case, each next line should be added with blanks so that the operator "." Has been leveled under the operator "=":



$sql = " SELECT 'id', 'name' FROM 'people' ". " WHERE 'name' = 'Susan' ". " ORDER BY 'name' ASC ";



A.4.3. Files


A.4.3.1. Files with numerical indexes


Negative numbers as indexes are forbidden.


Though the index of a file can begin with a negative number, but it is not welcomed and rekommenduetsja that all files began indexing with 0.


When the indexed file with the help of a design array is defined{determined}, the finishing blank should be added after each point for improvement chitabel`nosti:



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


Also it is authorized to define{determine} multilower case indexed files, using a design "array". In this case, each next line should be added with blanks so that the beginning of each line vyravneno was as shown lower:



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


A.4.3.2. Associative files

When the associative file with the help of a design "array" is defined{determined}, division of expression into some lines is welcomed. In this case, each next line should be added with the help of blanks so that both keys and values have been leveled:



$sampleArray = array ('firstKey' => 'firstValue', 'secondKey' => 'secondValue');



A.4.4. Classes


A.4.4.1. Definition of a class


Classes should be defined{determined} under the following circuit.


The brace is always written on the next line under a classname.


Each class should have the block documentation (doc-block) according to standard PHPDocumentor.


The code inside a class should have a space in four blanks.


Only one class is resolved{allowed} inside one PHP-file.


Accommodation in addition a code in a file with a class is authorized, but is not welcomed. In such files, two empty lines should divide{share} a class and an additional PHP-code.


It is an example of allowable definition of a class:



/ ** * the Doc-block here */class SampleClass {// contents of a class should be // with a space in four blanks}


A.4.4.2. Variables - members of classes


Variables - members of classes should be defined{determined} under the following circuit.


Any variables certain{determined} in a class should be certain{determined} in the beginning of a class, before definition of any method.


The keyword var is not authorized. Members of a class should define{determine} always their area of visibility, using a keyword private, protected or public. Access to variables - directly using a prefix public it is authorized to members of a class, but it is not welcomed for the benefit of methods of access (set/get).



A.4.5. Functions and methods


A.4.5.1. Definition of functions and methods


Functions should be defined{determined} under the following circuit.


Functions inside classes should define{determine} always the area of visibility with the help of one of prefixes private, protected or public.


As well as at classes, the brace is always written on the next line under a name of function. Blanks between a name of function and a round bracket for arguments - are absent.


Functions in global area of visibility extremely are not welcomed.


It is an example of allowable definition of function:



/ ** * the Doc-block here */class Foo {/** * the Doc-block here */public function bar () {// contents of a class should be // with a space in four blanks}}


The REMARK: Transfer under the link is allowable only in definitions of functions:



/ ** * the Doc-block here */class Foo {/** * the Doc-block here */public function bar (and $baz) {}}


Transfer under the link during a call is forbidden.


Returned value should not be framed in parentheses. Otherwise it worsens chitabel`nost`, and also mozhet` to break a code if the method begins to return result under the link later.



/ ** * the Doc-block here */class Foo {/** * it is BAD */public function bar () {return ($this-> bar);} / ** * it is GOOD */public function bar () {return $this-> bar;}}


A.4.5.2. Use of functions and methods

Arguments of function are divided{shared} by one finishing blank after each point. It is an example of an allowable call of function for function which accepts three arguments:



threeArguments (1, 2, 3);


Transfer under the link during a call is forbidden. Look section of definition of functions for a correct way of transfer of arguments of function under the link.


For functions, whose arguments suppose a file, the call of function can include a design "array" and can be divided{shared} into some lines for improvement chitabel`nosti. In this case, we apply the standard of the description of files:



threeArguments (array (1, 2, 3), 2, 3); threeArguments (array (1, 2, 3, 'Zend', 'Studio', $a, $b, $c, 56.44, $d, 500), 2, 3);



A.4.6. Managing structures


A.4.6.1. If / Else / Elseif


Managing structures based on if and elseif designs should have one blank up to an opening round bracket of a condition, and one blank after a closed round bracket.


Inside expression of a condition between parentheses operators should will divide{share} blanks for chitabel`nosti. Internal brackets are welcomed for improvement of a logic grouping of the big conditions.


The opening brace is written on the same line, as a condition. The closed brace is written on a separate line. All contents between brackets are written with a space to four blanks.



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


For expression "if", switching "elseif" or "else", formatting should be such, as in the following example:



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


PHP supposes a spelling of such expressions without braces under some conditions. The standard of coding does not do{make} distinctions - for all "if", "elseif" or "else" expressions it is necessary to use braces.


Use "elseif" designs is supposed, but extremely is not welcomed for the benefit of " else if " combinations.


A.4.6.2. Switch

Managing structures written with use "switch" designs should have one blank up to an opening round bracket conditional expression, and also one blank after a closed round bracket.


All contents between braces are written with a space to four blanks. Contents of everyone "case" expressions should be written with a space to additional four blanks.



switch ($numPeople) {case 1: break; case 2: break; default: break;}

[/CODE

]

The keyword default never should fall switch expression.


The REMARK: Sometimes it is useful to write case expressions which pass management following case to expression lowering{omitting} break or return. For distinction of such cases from mistakes, everyone case expression where it is lowered{omitted} break or return, should contain the comment " // break intentionally omitted ".


<h3> A.4.7. The built - in documentation </h3>


<b> A.4.7.1. A format of the documentation </b>


All blocks documentation ("doc-blocks") should be compatible with a format phpDocumentor. The description of a format phpDocumentor beyond the framework of given dokumeta. For in addition information look: http://phpdoc.org/


All files with initial codes written for Zend Frameworka or which operate with frejmvorkom should contain "file" doc-blocks in the beginning of each file and the "class" doc-block directly ahead of each class. Below examples of such doc-blocks.


<b> A.4.7.2. Files </b>

Each file containing a PHP-code should have the heading block in the beginning of the file, containing at least the following phpDocumentor-tegi:



/ ** * the Brief description of a file * * the Long description of a file (if is)... * * 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*/


A.4.7.3. Classes


Each class should have the doc-block containing at least the following phpDocumentor-tegi:



/ ** * the Brief description of a class * * the Long description of a class (if is)... * * @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 */


A.4.7.4. Functions


Each function, including methods of objects, should have the doc-block containing at least:


The description of function


All arguments


All possible{probable} returned values



There is no need to use teg "@access" because the area of visibility is already known from keywords "public", "private" or "protected." used at definition of function.


If the function / method can throw out exception, use teg @throws:



@throws exceptionclass [description]