在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
说起码代码,刚上大学那会,老师就教导我们,要严格,规范的,把代码写好。代码如人,工工整整。提起规范化的代码,从一开始用命令行编辑C语言代码就开始控制,强制自己按照相关的标准来,所以,现在写代码,不规范都不行,还是为当时打下的好习惯给自己点个赞。 现在写到了PHP,对于PHP,是否也有相关的代码规范呢,当然,你或许在阅读其他博客或者PHP相关文档的时候经常提到这几个名词,PSR-1,PSR-2之类的,这是PHP-FIG制定的推荐规范,今天,我们就来讲解下PHP的推荐标准,PSR(PHP Standards Recommendation)。 注:PHP-FIG已经废弃了第一份推荐规范,PSR-0,第一份推荐规范被新发布的PSR-4替代了。 PSR-1 : 基本的代码风格 :http://www.php-fig.org/psr/psr-1/ PSR-2 : 严格的代码风格 :http://www.php-fig.org/psr/psr-2/ PSR-3 : 日志记录器接口 :http://www.php-fig.org/psr/psr-3/ PSR-4 : 自动加载 :http://www.php-fig.org/psr/psr-4/ 上述网址需要连接vpn方能打开,没有vpn?去买一个吧,一年也就几十块钱。 今天给大家带来的主要是编码规范,PSR-1,PSR-2关于代码风格规范的。 PSR-1 基本代码风格总览
PHP标签PHP code MUST use the long 编码PHP code MUST use only UTF-8 without BOM. 作用(功能),类、方法、常量不能让人产生歧义A file SHOULD declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it SHOULD execute logic with side effects, but SHOULD NOT do both. The following is an example of a file with both declarations and side effects; i.e, an example of what to avoid: <?php // side effect: change ini settings ini_set('error_reporting', E_ALL); // side effect: loads a file include "file.php"; // side effect: generates output echo "<html>\n"; // declaration function foo() { // function body } The following example is of a file that contains declarations without side effects; i.e., an example of what to emulate: <?php // declaration function foo() { // function body } // conditional declaration is *not* a side effect if (! function_exists('bar')) { function bar() { // function body } } 命名空间和类名Each class is in a file by itself, and is in a namespace of at least one level: a top-level vendor name. The term “class” refers to all classes, interfaces, and traits(性状,类的部分实现).
Code written for PHP 5.3 and after MUST use formal namespaces. For example: <?php // PHP 5.3 and later: namespace Vendor\Model; class Foo { } 常量Class constants MUST be declared in all upper case with underscore separators. (全部大写,下面可以加下划线)For example: <?php namespace Vendor\Model; class Foo { const VERSION = '1.0'; const DATE_APPROVED = '2012-06-01'; } 变量This guide intentionally avoids any recommendation regarding the use of 方法Method names MUST be declared in PSR-2 严格代码风格用官网的话说,这是代码规范,PSR-1只是基本代码规范,如果大家只想要基本的,那下面的内容可以忽略哈,我个人建议还是按照此代码规范来。 总览
举例This example encompasses some of the rules below as a quick overview: <?php namespace Vendor\Package; use FooInterface; use BarClass as Bar; use OtherVendor\OtherPackage\BazClass; class Foo extends Bar implements FooInterface { public function sampleMethod($a, $b = null) { if ($a === $b) { bar(); } elseif ($a > $b) { $foo->bar($arg1); } else { BazClass::bar($arg2, $arg3); } } final public static function bar() { // method body } } 基础风格Code MUST follow all rules outlined in PSR-1. 文件All PHP files MUST use the Unix LF (linefeed) line ending. All PHP files MUST end with a single blank line. The closing 或许你之前也写过PHP的代码,这里,PHP文件的最后不许要有空行,而且不允许使用?>结尾标签。 代码行长度Lines SHOULD NOT be longer than 80 characters; lines longer than that SHOULD be split into multiple subsequent lines of no more than 80 characters each. 缩进Code MUST use an indent of 4 spaces, and MUST NOT use tabs for indenting. N.b.: Using only spaces, and not mixing spaces with tabs, helps to avoid problems with diffs, patches, history, and annotations. The use of spaces also makes it easy to insert fine-grained sub-indentation for inter-line alignment. 关键字PHP keywords MUST be in lower case. The PHP constants 命名空间When present, there MUST be one blank line after the When present, all There MUST be one There MUST be one blank line after the For example: <?php namespace Vendor\Package; use FooClass; use BarClass as Bar; use OtherVendor\OtherPackage\BazClass; // ... additional PHP code ... Extends和ImplementsThe <?php namespace Vendor\Package; use FooClass; use BarClass as Bar; use OtherVendor\OtherPackage\BazClass; class ClassName extends ParentClass implements \ArrayAccess, \Countable { // constants, properties, methods } 变量Visibility(public、private、protected) MUST be declared on all properties. The There MUST NOT be more than one property declared per statement. Property names SHOULD NOT be prefixed with a single underscore to indicate protected or private visibility. 这里是SHOULD NOT,我个人觉得应该是MUST NOT。 A property declaration looks like the following. <?php namespace Vendor\Package; class ClassName { public $foo = null; } 方法Visibility MUST be declared on all methods. Method names SHOULD NOT be prefixed with a single underscore to indicate protected or private visibility. 这里是SHOULD NOT,我个人觉得应该是MUST NOT。 The opening brace MUST go on its own line, and the closing brace MUST go on the next line following the body. <?php namespace Vendor\Package; class ClassName { public function fooBarBaz($arg1, &$arg2, $arg3 = []) { // method body } } 方法参数In the argument list, there MUST NOT be a space before each comma, and there MUST be one space after each comma. Method arguments with default values MUST go at the end of the argument list. <?php namespace Vendor\Package; class ClassName { public function foo($arg1, &$arg2, $arg3 = []) { // method body } } When the argument list is split across multiple lines, the closing parenthesis and opening brace MUST be placed together on their own line with one space between them. <?php namespace Vendor\Package; class ClassName { public function aVeryLongMethodName( ClassTypeHint $arg1, &$arg2, array $arg3 = [] ) { // method body } } abstract, final, and staticWhen present, the When present, the <?php namespace Vendor\Package; abstract class ClassName { protected static $foo; abstract protected function zim(); final public static function bar() { // method body } } 方法调用When making a method or function call, there MUST NOT be a space between the method or function name and the opening parenthesis, there MUST NOT be a space after the opening parenthesis, and there MUST NOT be a space before the closing parenthesis. In the argument list, there MUST NOT be a space before each comma, and there MUST be one space after each comma. <?php bar(); $foo->bar($arg1); Foo::bar($arg2, $arg3); 控制结构
if, elseif, elseAn <?php if ($expr1) { // if body } elseif ($expr2) { // elseif body } else { // else body; } switch, caseA <?php switch ($expr) { case 0: echo 'First case, with a break'; break; case 1: echo 'Second case, which falls through'; // no break case 2: case 3: case 4: echo 'Third case, return instead of break'; return; default: echo 'Default case'; break; } while, do while<?php while ($expr) { // structure body } <?php do { // structure body; } while ($expr); for, foreach<?php for ($i = 0; $i < 10; $i++) { // for body } <?php foreach ($iterable as $key => $value) { // foreach body } try, catch<?php try { // try body } catch (FirstExceptionType $e) { // catch body } catch (OtherExceptionType $e) { // catch body } 匿名函数Closures MUST be declared with a space after the The opening brace MUST go on the same line, and the closing brace MUST go on the next line following the body. There MUST NOT be a space after the opening parenthesis of the argument list or variable list, and there MUST NOT be a space before the closing parenthesis of the argument list or variable list. In the argument list and variable list, there MUST NOT be a space before each comma, and there MUST be one space after each comma. Closure arguments with default values MUST go at the end of the argument list. A closure declaration looks like the following. Note the placement of parentheses, commas, spaces, and braces: <?php $closureWithArgs = function ($arg1, $arg2) { // body }; $closureWithArgsAndVars = function ($arg1, $arg2) use ($var1, $var2) { // body }; 那些个参数换行的写法这里就不介绍了,感觉那样子会让代码乱糟糟的,个人不建议那么做,方法的参数一定要写注释,注释相当重要,宁愿舍弃点规范,也要多写注释,规范少写一点,大不了可读性差点,如果注释少些点,那就根本读不了了。 好了,今天关于PSR-1和PSR-2对于代码的规范就说到这,接下来我会给大家带来PSR-3日志规范和PSR-4自动加载器相关内容。良好的开端是成功的基本要素,希望大家能好好遵守代码规范,毕竟代码如人,工工整整嘛。^_^ |
2022-08-17
2022-11-06
2022-08-15
2022-08-17
2022-08-16
请发表评论