{"id":53,"date":"2017-03-20T17:53:34","date_gmt":"2017-03-20T09:53:34","guid":{"rendered":"https:\/\/www.unama.space\/?p=53"},"modified":"2017-03-20T17:55:30","modified_gmt":"2017-03-20T09:55:30","slug":"psr-1-and-psr-2","status":"publish","type":"post","link":"https:\/\/unama.website\/?p=53","title":{"rendered":"PSR-1 and PSR-2"},"content":{"rendered":"<h1>PSR:\u00a0PHP Standards Recommendations<\/h1>\n<p>PSR-1:\u00a0Basic Coding Standard<\/p>\n<p>PSR-2:\u00a0Coding Style Guide<\/p>\n<p>\u64c1\u6709\u5171\u540c\u5236\u5b9a\u7684Coding Style\u53ef\u4ee5\u8b93\u7a0b\u5f0f\u78bc\u66f4\u4f73\u5bb9\u6613\u7dad\u8b77\u3002<\/p>\n<h2>No Tabs<\/h2>\n<p>\u8acb\u75284 Spaces\u53d6\u4ee3tab\uff0c\u5982\u679c\u4f60\u662f\u4f7f\u7528vim\u53ef\u4ee5\u5728.vimrc\u52a0\u5165\uff1a<\/p>\n<pre class=\"lang:vim range:1-6 decode:true\">\" expand TABs to spaces                                                                                           \r\nset expandtab     \"et \r\n\" set TAB's width\r\nset tabstop=4     \"ts \r\n\" auto indent width\r\nset shiftwidth=4  \"sw<\/pre>\n<h2>Omit <code>?&gt;<\/code> and\u00a0end with a single blank line<\/h2>\n<p>\u958b\u982d\u53ef\u4f7f\u7528\u00a0<code>&lt;?php<\/code>\u00a0\u6216\u00a0<code>&lt;?=<\/code><code>\u00a0<\/code><\/p>\n<p>\u7d50\u675f\u6642\u4e0d\u9700\u8981\u00a0<code>?&gt;\u00a0<\/code>\u4e26\u7559\u4e00\u884c\u7a7a\u767d<\/p>\n<h2>\u00a0SHOULD NOT do both side effects and declarations<\/h2>\n<p>Side Effects\u4e0d\u8981\u8ddfDeclarations\u653e\u5728\u4e00\u8d77<br \/>\nSide Effects:<\/p>\n<pre class=\"lang:php range:1-9 decode:true \">&lt;?php\r\n\/\/ side effect: change ini settings\r\nini_set('error_reporting', E_ALL);\r\n\r\n\/\/ side effect: loads a file\r\ninclude \"file.php\";\r\n\r\n\/\/ side effect: generates output\r\necho \"&lt;html&gt;\\n\";<\/pre>\n<p>Declarations:\u00a0(classes, functions, constants, etc.)<\/p>\n<pre class=\"lang:php range:1-14 decode:true\">&lt;?php\r\n\/\/ declaration\r\nfunction foo()\r\n{\r\n    \/\/ function body\r\n}\r\n\r\n\/\/ conditional declaration is *not* a side effect\r\nif (! function_exists('bar')) {\r\n    function bar()\r\n    {\r\n        \/\/ function body\r\n    }\r\n}<\/pre>\n<h2>Leave one blank line after&#8230;<\/h2>\n<p>\u5728 namespace declaration \u548c use namespace \u4e0b\u65b9\u7559\u4e00\u884c\u7a7a\u767d<\/p>\n<pre class=\"lang:php range:1-8 decode:true\">&lt;?php\r\nnamespace Vendor\\Package;\r\n\r\nuse FooClass;\r\nuse BarClass as Bar;\r\nuse OtherVendor\\OtherPackage\\BazClass;\r\n\r\n\/\/ ... additional PHP code ...<\/pre>\n<h2>Opening braces for <strong>classes<\/strong> or <strong>methods<\/strong> MUST go on \u00a0the next line<\/h2>\n<pre class=\"lang:php range:1-15 decode:true \">&lt;?php\r\nclass ClassName\r\n{\r\n    public function sampleMethod($a, $b = null)\r\n    {\r\n        \/\/ method body\r\n    }\r\n\r\n    final public static function bar()\r\n    {\r\n        \/\/ method body\r\n    }\r\n}<\/pre>\n<ul>\n<li>Control structure keywords MUST have one space after them<\/li>\n<li>Opening braces for control structures MUST go on the same line, and closing braces MUST go on the next line after the body.<\/li>\n<li>Opening parentheses for control structures MUST NOT have a space after them, and closing parentheses for control structures MUST NOT have a space before.<\/li>\n<\/ul>\n<pre class=\"lang:php range:1-7 decode:true \">if ($a === $b) {\r\n    bar();\r\n} elseif ($a &gt; $b) {\r\n    $foo-&gt;bar($arg1);\r\n} else {\r\n    BazClass::bar($arg2, $arg3);\r\n}<\/pre>\n<p>class\u8207function \u548c ifelse\u7684{}\u662f\u4e0d\u540c\u7684<\/p>\n<h2>Method arguments with default values MUST go at the end of the argument list.<\/h2>\n<pre class=\"lang:php range:1-10 decode:true \">&lt;?php\r\nnamespace Vendor\\Package;\r\n\r\nclass ClassName\r\n{\r\n    public function foo($arg1, &amp;$arg2, $arg3 = [])\r\n    {\r\n        \/\/ method body\r\n    }\r\n}<\/pre>\n<p>See more in\u00a0<a href=\"http:\/\/www.php-fig.org\/\">http:\/\/www.php-fig.org\/<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PSR:\u00a0PHP Standards Recommendations PSR-1:\u00a0Basic Coding Standard PSR-2:\u00a0Coding St&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/unama.website\/?p=53\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/unama.website\/index.php?rest_route=\/wp\/v2\/posts\/53"}],"collection":[{"href":"https:\/\/unama.website\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unama.website\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unama.website\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unama.website\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=53"}],"version-history":[{"count":5,"href":"https:\/\/unama.website\/index.php?rest_route=\/wp\/v2\/posts\/53\/revisions"}],"predecessor-version":[{"id":60,"href":"https:\/\/unama.website\/index.php?rest_route=\/wp\/v2\/posts\/53\/revisions\/60"}],"wp:attachment":[{"href":"https:\/\/unama.website\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=53"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unama.website\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=53"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unama.website\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=53"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}