[PHP] Coding_Style
coding style
Coding Standards
3.2 version
edit this page
When contributing code to Symfony, you must follow its coding standards. To make a long story short, here is the golden rule: Imitate the existing Symfony code. Most open-source Bundles and libraries used by Symfony also follow the same guidelines, and you should too.
edit this page
When contributing code to Symfony, you must follow its coding standards. To make a long story short, here is the golden rule: Imitate the existing Symfony code. Most open-source Bundles and libraries used by Symfony also follow the same guidelines, and you should too.
Remember that the main advantage of standards is that every piece of code looks and feels familiar, it’s not about this or that being more readable.
Symfony follows the standards defined in the PSR-0, PSR-1, PSR-2 and PSR-4 documents.
這些規範都是為了統一各個框架各自造輪子的窘境。
這些規範都是為了統一各個框架各自造輪子的窘境。
Structure
Add a single space after each comma delimiter;
每個分號後面都加一個空白
Add a single space around binary operators (==, &&, …), with the exception of the concatenation (.) operator;
邏輯符號後面也要加空白
Place unary operators (!, --, …) adjacent to the affected variable;
Always use identical comparison unless you need type juggling;
Use Yoda conditions when checking a variable against an expression to avoid an accidental assignment inside the condition statement (this applies to ==, !=, =, and !);
Always use identical comparison unless you need type juggling;
Use Yoda conditions when checking a variable against an expression to avoid an accidental assignment inside the condition statement (this applies to ==, !=, =, and !);
Add a comma after each array item in a multi-line array, even after the last one;
陣列中每個元素後面都要加逗號,最後一個也要加。
Add a blank line before return statements, unless the return is alone inside a statement-group (like an if statement);
再回傳值之前先空一行,除非是單獨存在一個區塊中。
Use return null; when a function explicitly returns null values and use return; when the function returns void values;
Use braces to indicate control structure body regardless of the number of statements it contains;
Define one class per file - this does not apply to private helper classes that are not intended to be instantiated from the outside and thus are not concerned by the PSR-0 and PSR-4 autoload standards;
Declare the class inheritance and all the implemented interfaces on the same line as the class name;
Declare class properties before methods;
Declare public methods first, then protected ones and finally private ones. The exceptions to this rule are the class constructor and the setUp() and tearDown() methods of PHPUnit tests, which must always be the first methods to increase readability;
Declare all the arguments on the same line as the method/function name, no matter how many arguments there are;
Use parentheses when instantiating classes regardless of the number of arguments the constructor has;
Exception and error message strings must be concatenated using sprintf;
Calls to trigger_error with type E_USER_DEPRECATED must be switched to opt-in via @ operator. Read more at Deprecations;
Do not use else, elseif, break after if and case conditions which return or throw something;
Do not use spaces around [ offset accessor and before ] offset accessor.
Do not use spaces around [ offset accessor and before ] offset accessor.
Naming Conventions
Use camelCase, not underscores, for variable, function and method names, arguments;
Use underscores for option names and parameter names;
Use namespaces for all classes;
Prefix abstract classes with Abstract. Please note some early Symfony classes do not follow this convention and have not been renamed for backward compatibility reasons. However all new abstract classes must follow this naming convention;
Suffix interfaces with Interface;
Suffix traits with Trait;
Suffix exceptions with Exception;
Use alphanumeric characters and underscores for file names;
For type-hinting in PHPDocs and casting, use bool (instead of boolean or Boolean), int (instead of integer), float (instead of double or real);
Don’t forget to look at the more verbose Conventions document for more subjective naming considerations.
Service Naming Conventions¶
A service name contains groups, separated by dots;
The DI alias of the bundle is the first group (e.g. fos_user);
Use lowercase letters for service and parameter names;
A group name uses the underscore notation.
Documentation¶
Use underscores for option names and parameter names;
Use namespaces for all classes;
Prefix abstract classes with Abstract. Please note some early Symfony classes do not follow this convention and have not been renamed for backward compatibility reasons. However all new abstract classes must follow this naming convention;
Suffix interfaces with Interface;
Suffix traits with Trait;
Suffix exceptions with Exception;
Use alphanumeric characters and underscores for file names;
For type-hinting in PHPDocs and casting, use bool (instead of boolean or Boolean), int (instead of integer), float (instead of double or real);
Don’t forget to look at the more verbose Conventions document for more subjective naming considerations.
Service Naming Conventions¶
A service name contains groups, separated by dots;
The DI alias of the bundle is the first group (e.g. fos_user);
Use lowercase letters for service and parameter names;
A group name uses the underscore notation.
Documentation¶
Add PHPDoc blocks for all classes, methods, and functions;
Group annotations together so that annotations of the same type immediately follow each other, and annotations of a different type are separated by a single blank line;
Omit the @return tag if the method does not return anything;
The @package and @subpackage annotations are not used.
License¶
Group annotations together so that annotations of the same type immediately follow each other, and annotations of a different type are separated by a single blank line;
Omit the @return tag if the method does not return anything;
The @package and @subpackage annotations are not used.
License¶
Symfony is released under the MIT license, and the license block has to be present at the top of every PHP file, before the namespace.
參考
變數命名規則:
第一個字必須是字母貨_
變數名稱只能是英數_
變數名聲不能有空格,如有兩字元以上請用_
有大小寫之分
第一個字必須是字母貨_
變數名稱只能是英數_
變數名聲不能有空格,如有兩字元以上請用_
有大小寫之分
留言
張貼留言