?? ??? ??
? ??? [PSR-1][] ?? ?? ??? ?? ? ??? ????.
? ????? PHP ??? ????? ?? ??? ??? ????? ??? ???? ??? ??? ? ??? ?? ???? ?? ???? ???? ???? ?????.
?? ?????? ?? ?????? ???? ?? ?? ?? ??? ???? ? ?? ??? ??? ?? ?? ????? ?? ???? ?? ???? ?????. ??? ??? ? ??? ??? ? ??? ??? ?? ??? ??? ?? ???? ????.
??? "?? ??"("MUST"), "??? ? ??/??? ? ??"("MUST NOT"), "????"("??"), "? ???"("SHALL"), "SHALL NOT"), "SHOULD"), "SHOULD NOT", "RECOMMENDED", "MAY" ) ? "OPTIONAL")? [RFC 2119][]? ??? ???? ????.
??
?? ? [PSR-1][]? ?? ??? ??? ???.
?? ? ? ? ?? ?? 4?? ??? ?????? ???.
? ?? ?? ? ? 80? ??? ???? ???. ????? ? 120?? ??? ? ??? ? ?? ?????? ? ???.
? ?????? ?? ? ?? ?? ?? ?? ???? ?? ???? ???.
???? ?? ???({)? ?? ?? ?? ? ?? ???? ??, ?? ???(})? ? ?? ???? ???. ?? ?? ?? ?? ?? ????.
???? ?? ???({)? ?? ?? ?? ? ?? ???? ??, ?? ???(})? ???? ???. ?? ?? ?? ?? ?? ????.
??? ?? ? ??? ? ??? ???(??, ?? ? ??)? ???? ?? ?? ? ?? ? ??? ??? ?? ???? ?? ?? ? ??? ??? ?? ?????.
?? ??? ??? ?? ?? ?? ??? ??? ???, ???? ??? ??? ? ?? ?? ??? ??? ???.
?? ??? ?? ???({)? ??? ?? ?? ???? ??, ?? ???(})? ?? ?? ?? ?? ???? ???.
?? ??? ?? ?? ?? ?? ?? ??? ?? ??? ??? ??? ???.
1.1. ?
?? ?? ????? ? ??? ???? ???? ?????.
namespace VendorPackage;use FooInterface;- BarClass? Bar? ??;
- OtherVendorOtherPackageBazClass ??;
- class Foo? Bar? ???? FooInterface? ?????
- {
- ?? ?? SampleFunction($a, $b = null)
- {
- if ($a === $b) {
- bar();
- } elseif ($a > $b) {
- $foo->bar($arg1);
- } else {
- BazClass::bar($arg2, $arg3);
- }
- }
-
- final public static function bar()
- {
- // ??? ??
- }
- }
-
-
- ?? ??
-
?? ??
2.1 ?? ?? ??
?? ? [PSR-1][]? ?? ??? ???? ???.
2.2 ??
?? PHP ??? ? ???? Unix LF(????)? ???? ???.
?? PHP ??? ? ?? ??? ???.
?? PHP ?? ??? ??? ?> ?? ??? ???? ???.
2.3. ??
? ?? ?? ??? ??? ???? ? ???.
??? ?? ?? ? 120?? ????? ???. ? ??? ???? ?? ?? ?? ??? ???? ??? ? ??? ???? ??? ??? ? ??? ?? ???? ?????.
? ? ? 80?? ??? ? ????. 80?? ???? ?? ?? ?? ??? ???.
??? ?? ? ??? ?? ??? ??? ???.
? ?? ???? ??? ? ?? ?? ? ?? ??? ????? ??? ? ??? ???.
? ??? ? ? ??? ??? ??? ? ????.
2.4. ????
?? ? 4?? ???? ??????? ??, ? ?? ????? ? ??? .
??: ????? ? ? ?? ??? ???? ?? ?? ??, ?? ??, ?? ?? ??, ?? ?? ? ??? ?? ? ??? ??? ????. ?? ????? ??? ???? ??? ? ?????.
2.5. ??? ? True/False/Null
PHP ?? [???][]?? ????? ???.
true, false ? null ??? ?? ????? ???.
?????? ? ?? ??
?????? ?? ??? ? ?? ???? ???.
?? ??? ?????? ?? ???? ???.
? use ??? ??? use ???? ??? ???.
use ? ??? ??? ??? ? ?? ??? ???.
?:
?????? VendorPackage;- FooClass ??;
- BarClass? Bar? ??;
- OtherVendorOtherPackageBazClass ??;
- // ... ?? PHP ?? ...
?? ?? ???, ?? ? ???
??? "???"? ??? ??? ?? ???, ????? ? ?? ?? ??? ?????.
4.1. ??? ??
?? ? ?? ???? ??? ??? ?? ?? ???? ???.
???? ?? ??? ? ? ?? ???? ??, ?? ??? ? ??? ?? ??? ? ?? ???? ???. ?????? VendorPackage; - FooClass ??;
- BarClass? Bar? ??;
- OtherVendorOtherPackageBazClass ??;
- class ClassName extends ParentClass Implements ArrayAccess, Countable
- {
- // ??, ??, ???
- }
- ?? ??
??? ?? ??? ?? ?? ?? ?? ???? ? ?? ??? ? ????? ?? ? ? ?? ?? ???? ??? ?? ????? ???. ?????? VendorPackage; - FooClass ??;
- BarClass? Bar? ??;
- OtherVendorOtherPackageBazClass ??;
- class ClassName extends ParentClass Implements
- ArrayAccess,
- Countable,
- Serialized
- {
- // ??, ??, ???
- }
- ?? ??
4.2.
? ???? ??? ??? ???? ???? ??? ???.
??? ???? ?? var ???? ???? ? ???.
? ?? ? ? ??? ??? ???? ? ???.
??? ????? ?? ????? ???? ?? ??? ???? ???? ???.
??? ?? ??? ????. namespace VendorPackage; - class ClassName
- {
- public $foo = null;
- }
- ?? ??
4.3.
?? ???? ??? ???? ???? ???.
???? ????? ?? ????? ???? ?? ??? ???? ???? ??? .
??? ?? ?? ?? ?? ??? ???? ? ???. ?? ??? ? ? ?? ???? ??? ??, ?? ??? ? ??? ???. ??> ??? ?? ?? ??? ?? . ????? ?? ?? ?? ??? ?? ??? ?? ? ??? ???.
?? ??? ??? ?? ??, ??, ?? ? ??? ??? ?????. ?????? VendorPackage; - class ClassName
- {
- ?? ?? fooBarBaz($arg1, &$arg2 , $arg3 = [])
- {
- // ??? ??
- }
- }
- ?? ??
4.4.
???? ???? ? ?? ??? ?? ? ??? ??, ?? ??? ??? ???? ? ???.
???? ?? ????? ? ???? ??? ???? ????? ???. - ?????? VendorPackage;
- class ClassName
- {
- ?? ?? foo($arg1, &$arg2 , $arg3 = [])
- {
- // ??? ??
- }
- }
?? ?????? ?? ?? ?? ?? ? ???? ? ?? ????? ??? ? ???? ? ??? ?? ??? ???.
???? ??? ?? ?? ?? ? ???? ?? ??? ?? ???? ?? ?? ???? ???? ???? ???. - ?????? VendorPackage;
- class ClassName
- {
- ?? ?? aVeryLongMethodName(
- ClassTypeHint $arg1 ,
- &$arg2,
- array $arg3 = []
- ) {
- // ??? ??
- }
- }
?? ?? 4.5 ????? ????? ?????.
?? ?? ?? ??? ???? ?? ???? ?? ??? ?? ? ?? ??, static? ?? ?? ? ?? ???. - ?????? VendorPackage;
- ?? ??? ClassName
- {
- protected static $foo;
- ?? ?? ?? zim();
- final public static function bar()
- {
- // ??? ??
- }
- }
?? ?? 4.6.
??? ? ?? ?? ?, ??? ?? ?? ?? ??? ???? ? ?? ?? ??? ??? ??? ??, ??? ?? ??? ??? ??? ???. ???? . ? ???? ??? ??? ??? ???, ??? ??? ??? ???. bar();- $foo->bar($arg1);
- Foo::bar($arg2 , $arg3);
-
?? ??
????? ?? ?? ?? ? ???, ? ? ? ?? ????? ??? ? ???? ? ???? ? ? ?? ??? ???.
$foo->bar(- $longArgument,
- $longerArgument,
- $muchLongerArgument
- ) ;
-
?? ??
?? ??
????? ?? ??? ??? ????.
?? ?? ??? ??? ??? ??? ???.
?? ?? ??? ??? ??? ???( .
??? ??? ??? ??? ??? ???.
??? ?? )? ?? ??? { ??? ?? ? ??? ???.
??? ? ??? ? ? ??????? ???.
??? ?? } ??? ?? ?? ??? ?? ??? ???.
? ??? ?? ? ??? ??? ??? ???. ??? ?? ??? ?? ????? ? ?? ??? ? ??? ??? ???? ?????.
5.1. if , elseif ? else
?? if ??? ?? ??? ????. ???, ?? ? ???? ??? ?????. else? elseif? ?? ?? ???? ?? ?? ????.
if ($expr1) {
// if body } elseif ($expr2) { // elseif body- } else {
- // else body;
- }
- ?? ??
??? elseif? ???? ???. ?? ?? ???? ?? ??? ????? ??? else? ?? ?????.
5.2.???? ???
?? ??? ??? ?? ??? ????. ??, ??, ???? ??? ?????. case ? ? ???? ???? ??????? ??, break ?? Case ?? ?? ?? ??? ???? ??????? ???. ?? ?? ?? ?? ?? ???? ?? ?? // no break in the body? ??? ??? ??? ???.
???($expr) {
??? 0: echo '? ?? ???, ?? ??'; break;- ?? 1:
- echo '???? ? ?? ??';
- // ?? ??
- ?? 2:
- ?? 3:
- ?? 4:
- echo '? ?? ??, break ?? return';
- return;
- default:
- echo 'Default case';
- break;
- }
- ?? ??
- 5.3. ??? ?? ??
?? while ?? ??? ??? ???. ???, ?? ? ???? ??? ?????. - while ($expr) {
- // ??? ??
- }
?? ?? ?? do while?? ??? ????. ???? ???, ??, ???? ??? ?????. - do {
- // ??? ??;
- } while ($expr);
?? ?? 5.4.
?? ??? ??? ??? ??, ??, ???? ??? ?????. - for ($i = 0; $i < 10; $i ) {
- // ??
- }
?? ?? 5.5.
?? foreach ?? ??? ????. ???, ??, ???? ??? ?????. - foreach ($iterable as $key => $value) {
- // foreach ??
- } ?? ??5.6.
?? try catch ?? ??? ????. ???, ?? ? ???? ??? ?????.
-
- try {
- // try body
- } catch (FirstExceptionType $e) {
- // catch body
- } catch (OtherExceptionType $e) {
- // ?? ??
- }
?? ??
???
??? ?? ? ??? ?? ?, ??? ?? ??? ??? ??? ???.
?? ??? ? ???? ?? ?? , ?? ??? ? ??? ??? ? ?? ?? ?? ???.
???? ??? ?? ????? ?? ?? ?, ??? ?? ?? ??? ??? ???.
???? ? ?? ???? ?? ?? ?? ??? ??? ? ??, ?? ?? ?? ??? ??? ???.
???? ???? ?? ???? ? ?? ?? ????? ???.
?? ??? ???? ??? ??? ??, ??, ??, ???? ??? ?????.
$closureWithArgs = function ($arg1, $arg2) { - // ??
- };
-
- $closureWithArgsAndVars = function ($arg1, $arg2) use ($var1, $var2) {
- // body
- };
-
-
?? ??
???? ??? ?? ?? ? ?? ?? ?? ? ???? ? ?? ??? ??? ? ????? ??? ??? ?? ??? ??, ?? ??? ??? ?? ??? ?????. ???? ?? ??? ? ?? ?? ????? ???.
?? ??? ???? ? ?? ??? ?? ?? ???? ?? ??? ???? ????.
$longArgs_noVars = ??(
- $longArgument,
- $longerArgument,
- $muchLongerArgument
- ) {
- // ??
- };
-
- $noArgs_longVars = function () ?? (
- $longVar1,
- $longerVar2,
- $muchLongerVar3
- ) {
- / / body
- };
-
- $longArgs_longVars = ??(
- $longArgument,
- $longerArgument,
- $muchLongerArgument
- ) ??(
- $longVar1,
- $longerVar2,
- $muchLongerVar3
- ) {
- // ??
- };
-
- $longArgs_shortVars = ??(
- $longArgument,
- $longerArgument,
- $muchLongerArgument
- ) ??($var1) {
- // ??
- };
-
- $shortArgs_longVars = ??($arg) ??(
- $longVar1,
- $longerVar2 ,
- $muchLongerVar3
- ) {
- // body
- };
-
?? ??
???? ?? ????? ?? ?????. ?? ??? ??? ???? ?? ??? ??? ? ??? ?????.
$foo->bar(- $arg1,
- ??($arg2) ??($var1) {
- // ??
- },
- $arg3
- );
-
?? ?? ??
? ???? ??? ???? ?? ???? ?? ?? ??? ????? ????.
?? ?? ? ?? ??
??? ??
???? ??
??? ??
?? ? ?? ??
????? ???? ???
?? ??
? ??? ?? ?? ? ??? ?? ??? ??? ????.
?? A. ???
? ??? ???? ?? ???? ? ??? ????? ?? ??? ?? ??? ???? ?? ???? ??????. ??? ?? ?? ??? ????? ???? ??? ? ????.
A.1. ??? ???
- url,http://www.horde.org/apps/horde/docs/CODING_STANDARDS,http://pear.php.net/manual/ en/standards.php,http://solarphp.com/manual/appendix-standards.style,http://framework.zend.com/manual/en/coding-standard.html,http://symfony.com/ doc/2.0/contributing/code/standards.html,http://www.ppi.io/docs/coding-standards.html,https://github.com/ezsystems/ezp-next/wiki/codingstandards,http: //book.cakephp.org/2.0/en/contributing/cakephp-coding-conventions.html,https://github.com/UnionOfRAD/lithium/wiki/Spec:-Coding,http://drupal.org/coding -??,http://code.google.com/p/sabredav/,http://area51.phpbb.com/docs/31x/coding-guidelines.html,https://docs.google.com/a/ zikula.org/document/edit?authkey=CPCU0Us&hgd=1&id=1fcqb93Sn-hR9c0mkN6m_tyWnmEvoswKBtSc0tKkZmJA,http://www.chisimba.com,n/a,https://github.com/Respect/project-info/blob/master/ coding-standards-sample.php,n/a,PHP? ?? ??,http://doc.nette.org/en/coding-standard,http://flow3.typo3.org,https://github.com /propelorm/Propel2/wiki/Coding-Standards,http://developer.joomla.org/coding-standards.html
- ??,?,?,?,?,?,?,?,?,?,? ,?,?,?,?,?,???,???,???,?,?,???,?
- indent_type,4,4,4,4,4,tab,4,tab,tab,2,4 ,?,4,4,4,4,4,4,?,?,4,?
- line_length_limit_soft,75,75,75,75,no,85,120,120,80,80,80,no,100,80 ,80,?,?,120,80,120,no,150
- line_length_limit_hard,85,85,85,85,no,no,no,no,100,?,no,no,no,100,100,?,120,120 ,no,no,no,no
- class_names,studly,studly,studly,studly,studly,studly,studly,studly,studly,studly,studly,lower_under,studly,lower,studly,studly,studly,studly,? ,????,????,????
- class_brace_line,??,??,??,??,??,??,??,??,??,??,??,??,??,??,??,??,??,??,?? ,??,??
- ??_??,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper ,upper
- true_false_null,lower,lower,lower,lower,lower,lower,lower,lower,lower,upper,lower,lower,lower,upper,lower,lower,lower,lower,lower,upper,lower,lower
- ??_??,??, ??, ??, ??, ??, ??, ??, ??, ??, ??, ??, lower_under, ??, ??, ??, ??, ??, ??, ??, ??, ??, ??
- method_brace_line,??,??,??,??,??,??,??,??,??,??,??,??,??,??,??,??,??,??,??,??,??,??
- control_brace_line ,??,??,??,??,??,??,??,??,??,??,??,??,??,??,??,??,??,??,??,??,??,??
- control_space_after,yes ,?,?,?,?,???,?,?,?,?,???,?,?,?,?,?,?,?,?,?,?
- always_use_control_braces,?,? ,?,?,?,?,???,?,?,?,???,?,?,?,?,???,?,?,?,?,?,?
- else_elseif_line,??,??,?? ,??,??,??,??,??,??,??,??,??,??,??,??,??,??,??,??,??,??,??
- case_break_indent_from_switch,0/1,0/1 ,0/1,1/2,1/2,1/2,1/2,1/1,1/1,1/2,1/2,1/1,1/2,1/2,1 /2,1/2,1/2,1/2,0/1,1/1,1/2,1/2
- function_space_after,no,no,no,no,no,no,no,no ,no,no,no,no,no,no,no,no,no,no,no,no,no,no
- closing_php_tag_required,no,no,no,no,no,no,no,no,yes ,???,???,???,???,?,???,???,???,???,???,?,???,???
- line_endings,LF,LF,LF,LF,LF,LF,LF,LF,?,LF ,?,LF,LF,LF,LF,?,,LF,?,LF,LF,LF
- static_or_visibility_first,static,?,static,either,either,either,visibility,visibility,visibility,either,static, ?? ?,?,???,?,?,? ?,? ?,???,???,??,?
- control_space_parens,no,no,no,no,no,no,yes,no,no,no,no,no, no,yes,?,no,no,no,no,no,no,no
- blank_line_after_php,no,no,no,no,yes,no,no,no,no,yes,yes,no,no, ?,?,?,?,???,?,???,?,???
- class_method_control_brace,??/??/??,??/??/??,??/??/??,??/??/??,??/??/ ??, ??/??/??, ??/??/??, ??/??/??, ??/??/??, ??/??/??, ??/??/??,??/??/??,??/??/??, ??/??/??,??/??/??,??/??/??,??/??/??,??/??/??,??/??/??,??/??/??,??/??/??,??/??/??,??/ ??/??
-
?? ??
A.2. ??? ??
indent_type: ???? ??. tab = "? ?? ? ? ??", 2 ?? 4 = "?? ?"
line_length_limit_soft: ? ?? ?? ?? ?? "???" ?? ? = ?? ?? ?? ?? ??, ???? ??? ??? ?????.
line_length_limit_hard: ? ?? ?? ?? ?? "??" ?? ? = ?? ?? ?? ?? ??? ??? ??? ?????.
class_names: ??? ??. lower = ???? ??, lower_under = ??? ??? ???, StudlyCase ?? ??? ???.
class_brace_line: ???? ?? ???? class ???? ?? ?? ???, ??? ?? ?? ????
constant_names: ???? ?? ??? ???? ??? ?????? ??? = ??? ??? ???.
true_false_null: ??? true, false ? null? ?? ?????? ??? ?? ???????
method_names: ??? ??? ??? ?????? camel = camelCase, lower_under = ??? ??? ???.
method_brace_line: ???? ?? ???? ??? ??? ?? ?? ????, ??? ?? ?? ?????
control_brace_line: ?? ??? ?? ???? ??? ?? ?? ????, ??? ?? ?? ?????
control_space_after: ?? ?? ??? ?? ??? ????
always_use_control_braces: ?? ??? ?? ???? ?? ????
else_elseif_line: else ?? elseif? ?? ?? ???? ?? ?? ????, ??? ?? ?? ?????
case_break_indent_from_switch: switch ??? case? break? ???? ?? ?????? ?? ??? ? ?????
function_space_after: ?? ????? ?? ??? ?? ??? ?? ?? ??? ??? ????
closing_php_tag_required: ?? PHP ??? ??? ??? ?? ?> ?? ??? ??????
line_endings: ?? ??? ? ?? ???? ????
static_or_visibility_first: ?? ???? ??? ? static? ??? ??? ??? ?? ???? ????
control_space_parens: ?? ???? ?? ?? ?? ??? ?? ?? ??? ???? ? = if( $expr ), no = if($expr).
blank_line_after_php: PHP ?? ?? ?? ? ?? ??????
class_method_control_brace: ???, ??? ? ?? ???? ??? ??? ?????.
A.3. ??? ?? ??
- indent_type:
- ?: 7
- 2: 1
- 4: 14
- line_length_limit_soft:
- ?: 2
- ???: 3
- 75:4
- 80:6
- 85:1
- 100:1
- 120:4
- 150:1
- line_length_limit_hard:
- ?: 2
- no: 11
- 85: 4
- 100: 3
- 120: 2
- class_names:
- ?: 1
- lower: 1
- lower_under: 1
- Studly: 19
- class_brace_line:
- next: 16
- same: 6
- constant_names:
- upper: 22
- true_false_null:
- lower: 19
- upper: 3
- method_names:
- camel: 21
- lower_under: 1
- method_brace_line:
- next: 15
- same: 7
- control_brace_line:
- next: 4
- same : 18
- control_space_after:
- ???: 2
- ?: 20
- always_use_control_braces:
- ???: 3
- ?: 19
- else_elseif_line:
- ??: 6
- ??: 16
- case_break_indent_from_switch:
- 0/1: 4
- 1/1: 4
- 1/2: 14
- function_space_after:
- ???: 22
- closing_php_tag_required:
- ???: 19
- ?: 3
- line_endings:
- ?: 5
- LF: 17
- static_or_visibility_first:
- ?: 5
- ? ? ??: 7
- ??: 4
- ???: 6
- control_space_parens:
- ?: 1
- ???: 19
- ?: 2
- blank_line_after_php:
- ?: 1
- ???: 13
- ? : 8
- class_method_control_brace:
- ??/??/??: 4
- ??/??/??: 11
- ??/??/??: 1
- ??/??/??: 6
-
?? ??
Github(PizzaLiu)?? ???
|