Home > Article > Backend Development > PSR specification of PHP specification
This article introduces the psr specification in the PHP specification. Now I share it with you. Interested friends can take a look.
Reference: http://psr .phphub.org/
PSR is not an official PHP standard, but a series of standards extracted from well-known PHP projects such as Zend and Symfony2 , there are currently more and more community projects joining and following this standard. <br>
<br>
<br>
#PSR- 0 Autoloading 2 Code style
PSR-3 log interface
PSR-4 How to specify the file Path to automatically load class definition
PSR-1 Basic Code Specification
PHP interoperability.
##· PHPCode file
must
ends with or = <span style="color:#858080;background:#F9FAFA;"></span>Start of tag;
##· Must<span style="color:#858080;background:#F9FAFA;"></span>
with
BOM UTF-8 Encoding;<span style="color:#858080;background:#F9FAFA;"></span>·
PHP<span style="color:#858080;background:#F9FAFA;"></span>
<span style="color:#858080;background:#F9FAFA;"></span>
<span style="color:#858080;background:#F9FAFA;"></span>
##Only define classes, functions, constants, etc.Statements, or others will produce Side effects Operations (such as: generating file output and modifying .ini Configuration files, etc.), you can only choose one of the two; <span style="color:#FF0000;background:#F9FAFA;"></span>
Namespace and class
· Class naming must follow <span style="color:#858080;background:#F9FAFA;">StudlyCaps</span>
Camel case starting with capitalNaming convention;
##· Each class is an independent file, and the namespace has at least one level: the top-level organization name (vendorname)
Class attribute: Lowercase starting camel case $studlyCaps<span style="color:#858080;background:#F9FAFA;"></span>
Class method: lowercase beginning with camel case
##Constant
##·Constants in the class have all letters must is in uppercase letters, and words are separated by underscores;
conform to camelCase style naming convention of camel case starting with lowercase. <span style="color:#858080;background:#F9FAFA;"></span>
##PSR-2 Coding Style Specification
##· You must use
4 spaces instead of "Tab keys ” to indent. Lines The number of characters per line should be softly maintained at 80
Within , there must not be more than 120, but there must not be a hard limit.
PHP filesmustbegin with a blank line
As an end.Each line must be cannotThere must be more than one statement
namespace namespace declaration statement and use declaration block, a blank line must be inserted.
##The opening brace ({) of class must be written after the function declaration on its own line , the closing curly brace (}) must also be written after the function body on its own line
.The opening curly brace ({) of the method must be written after the function declaration is on its own line , and the closing brace Brackets (}) must also be written after the function body on its own line .
The opening brace ({) of the control structure must be written on the same line as the declaration, and the closing curly brace (}) must be written after the body on its own line .
##Modifier
Class properties and methods must add access modifiers (private,protected and public), abstract and final must be declared before access modifiers, while static must is declared after the access modifier.
##Space
Class method parametersAfter each commamustThere must be a space, and the comma There must be no spaces in front
There must be no spaces.
Keywords
##·Control structure There must be a space character after the keyword , but when calling a method or function, there must be no . All PHP keywords must be all lowercase, and the constants true, false, and null must also be all lowercase.
<span style="color:#858080;background:#F9FAFA;">#LoggerInterface</span>
The interface is defined externally Eight methods are used to record the eight levels of logs defined in RFC 5424 : debug 、 info、 notice、 warning、 error, critical, alert and emergency .
The ninth method——log, its first parameter is Recorded level. This method can be called using a predefined level constant as a parameter, must has the same effect as directly calling the above eight methods. If the level constant parameter passed in is not predefined, must throwsPsr\Log\InvalidArgumentException type of exception. In cases of uncertainty, users should not use unsupported level constants to call this method.
##/**
* Log level constant definition
*/
##class LogLevel
##{
constEMERGENCY ='emergency';
##constALERT ='alert';
constCRITICAL ='critical';
constERROR ='error'; constWARNING ='warning'; constNOTICE = 'notice'; constINFO ='info'; constDEBUG ='debug'; } Similar to the following example: # #1. Fully compliant class names must have a top-level namespace (Vendor Name) 2. Fully compliant class names can have multiple sub-namespaces 3. Completely Compliant class names should have a terminating class name 4. Underscores have no special meaning in fully compliant class names 5. Letters can be any combination of upper and lower case in fully compliant class names 6. All class names must be quoted in a case-sensitive manner 7. The termination class name corresponds to a file ending with .php . The file name must match the case of the terminating class name Some common customization specifications 2. sql statement, 3 or more lines of logical code, blank lines 3. No blank lines between contextual functions or methods 4. Bool uses if (true === $name) in the form 5. Use double quotes to link variables 6. Pay attention to implicit conversion
##/** * Overall description of global variables<br> * Global STATUS_CODE * 0 -SUCCESS<br> * 2-ERROR<br>*/<br>STATUS_CODE<br><br> 8. The function header has the following comments: /*************************************************<br> Function: // Chinese name of function<br>Description: // Description of function functions, performance, etc.<br>Calls: // List of functions called by this function<br>Called By: // List of functions that call this function <br>Table Accessed: // The accessed table (this item is only for programs involving database operations) <br>Table Updated: // The modified table (this item is only for programs involving database operations) Program to operate on the database) <br>Input: // Input parameter description, including the role of each parameter, value description and the relationship between parameters. <br>Output: // Description of the output parameters. <br>Return: ## ***********************/<br><br> 9. Comments Blank lines
// code one comments<br>program code one <br><br> // code two comments<br>program code two ##10. File header comments ##/***************************************************** ******** Copyright (C), 1988-1999, Huawei Tech. Co., Ltd. // CopyrightFileName: test.cpp File name<br>Author: Description: Main functions and their functionsHistory: init<br> ********** ****************************************************/<br><br><br><br> <br><br><br><br>11. The case jump of the switch statement requires comments <br> temp can be abbreviated as tmp ;<br>flag can be abbreviated to flg ;<br>##statistic can be abbreviated to stat ;<br>increment can be abbreviated as inc ;<br>message Can be abbreviated as msg ; ##13. Naming prefix of the interface
add / remove ##end create / destroyinsert /<br>delete first / last get / releaseincrement /<br>decrement put / getadd /<br>delete lock / unlock open / closemin /##max old <br>/ new start / stopnext /previous source <br> / target show / hidesend /receive source <br>/ destinationcut / paste up <br>/ down ##14. Try to use constants for status codes instead of hard-to-understand numbers should be changed to the following form. <br>#defineTRUNK_IDLE 0<br>#define TRUNK_BUSY 1<br> <br>if (Trunk[index].trunk_state == TRUNK_IDLE)<br>{<br> Trunk [index].trunk_state = TRUNK_BUSY;<br> ... //program code<br>} 15. Class file names use first capital camel case, Ordinary files are named using underline format 16. Variables in the configuration are named using underline format ## ##1. If you can define the class method as static, try to define it as static, its speed will increase nearly 4 times. 2, The speed of $row['id'] is $row[id] of 7 times. ##3##、 echo is faster than print, and uses echo's multiple parameters(Translation Note: Refers to Commas instead of periods ) instead of string concatenation, like echo $str1 ,$str2. ##4##, executing for Determine the maximum number of loops before looping. Do not calculate the maximum value every loop. It is best to use foreach replace. ##5 ##. Unregister those unused variables, especially large arrays, for release Memory. 6, try to avoid using __get, __set##, __autoload . ##7##、 require_once()#is expensive. 8, Try to use absolute paths when including files, because it avoids PHP The speed of finding files in include_path will take less time to parse the operating system path. ##9##, If you want to know when the script starts executing(Translation Note: The server receives the client request )# At the ## moment, it is better to use $_SERVER['REQUEST_TIME'] than time(). , function replaces regular expression to complete the same function. ##11 ##、## The str_replace function is faster than the preg_replace function, # The efficiency of the ##str function is four times that of the str_replace function times. 12 ##13, use the selective branch statement (Translation Note: switch case) is better to use Multiple if,##else if Statement. ##14##, use #The practice of blocking error messages is very inefficient, extremely inefficient. ##15##, open ## The mod_deflate module of #apache## can improve the browsing speed of web pages. 16. The database connection should be closed when finished using it. Do not use long connections. 17##, Error messages are expensive. ##18##、AvoidincrementGlobal VariableRelated recommendations: About PSR-4 of PHP specification About Lei Feng’s handwritten newspaper About the Spring Festival Handwritten newspaper about the Spring Festival Ancient PHP specification PSR-1 php language php manual php development engineer The above is the detailed content of PSR specification of PHP specification. For more information, please follow other related articles on the PHP Chinese website!##PSR-4 automatic loading specification
Control layer
1. No sql statement appears (sql is encapsulated into the model layer and then called as a method)
1. 变量自增自减写到表达式前面: --e >= $name
2. 程序顺序应该遵循:参数定义代码->判断逻辑代码->业务逻辑代码
3. 大的数据使用引用
4. 为逻辑代码里每个复杂度高/重复度高的代码添加注释
5. 尽量不为简短代码引入新的变量,减少变量污染
<br>