PSR是PHP Standards Recommendation的简称,这个是php-fig组织制定的一套规范。php
PSR-1html
?>
关闭标签,能够避免意料以外的输出错误,若是加上关闭标签,且在关闭标签后有空行,那么空行会被当成输出,致使意想不到的错误。
true
,
false
, 和
null
也必须小写
use
声明:
namespace
声明以后必需要有一个空行,并且
use
声明必须放在
namespace
以后,必须分别使用
use
引入命名空间,并且
use
后要有空行
namespace Vendor\Package; use FooClass; use BarClass as Bar; use OtherVendor\OtherPackage\BazClass; // ... additional PHP code ...
类的继承和实现:extends
和implements
关键字必须和类名在同一行,类、接口和Traits
定义体的起始括号应该在类名以后新起一行,结束括号也必须新起一行,例如框架
namespace Vendor\Package; use FooClass; use BarClass as Bar; use OtherVendor\OtherPackage\BazClass; class ClassName extends ParentClass implements \ArrayAccess, \Countable { // constants, properties, methods }
若是implements
后面后不少类致使一行很长,能够依次将须要的类另起新行并缩进4个空格函数
namespace Vendor\Package; use FooClass; use BarClass as Bar; use OtherVendor\OtherPackage\BazClass; class ClassName extends ParentClass implements \ArrayAccess, \Countable, \Serializable { // constants, properties, methods }
可见性:
类中的每一个属性和方法都要声明可见性,有public
、private
和protected
,不能使用var
关键词来声明,老版本的PHP会在私有属性前加上_
,一行只能声明一个属性ui
方法:
类中的全部方法也应该定义可见性,方法名后面不能有空格,方法体的括号位置和类定义体的括号位置同样,都要新起一行,结束括号也要新起一行。方法参数的起始圆括号以后没有空格,结束括号以前也没有空格,有多个参数是,每一个参数的逗号后面加一个空格,例如:
namespace Vendor\Package; class ClassName { public function fooBarBaz($arg1, &$arg2, $arg3 = []) { // method body } }
若是参数比较多,须要换行时,能够以下
namespace Vendor\Package; class ClassName { public function aVeryLongMethodName( ClassTypeHint $arg1, &$arg2, array $arg3 = [] ) { // method body } }
abstract
、final
和static
:
如今,abstract
、final
必须在可见性修饰符以前,static
声明必须放在可见性修饰符以后,例如:
namespace Vendor\Package; abstract class ClassName { protected static $foo; abstract protected function zim(); final public static function bar() { // method body } }
方法和函数的调用:
在调用方法和函数时,圆括号必须跟在函数名以后,函数的参数之间有一个空格:
bar(); $foo->bar($arg1); Foo::bar($arg2, $arg3);
若是参数比较多,一行放不下时,以下处理:
$foo->bar( $longArgument, $longerArgument, $muchLongerArgument );
$gorilla = new \Animals\Gorilla; $ibis = new \Animals\StrawNeckedIbis; if ($gorilla->isWake() === true) { do { $gorilla->beatChest(); } while ($ibis->isAsleep() === true); $ibis->flyAway(); }
PHP闭包函数:
闭包函数在声明时,function
关键词后必须有一个空格,同时use
关键词先后也必须有一个空格。起始大括号不须要另起新行,详细的以下代码:
$closureWithArgs = function ($arg1, $arg2) { // body }; $closureWithArgsAndVars = function ($arg1, $arg2) use ($var1, $var2) { // body };
PSR-3规定了一套通用的日志记录器接口
为了符合PSR-3规范,框架必须实现该规范中的接口,这样能够更多的兼容第三方应用。PSR-3规范中包含了9个方法,每一个方法都对应了RFC 5424协议的一个日志级别,并且都接受两个参数$message
和$context
<?php namespace Psr\Log; /** * Describes a logger instance * * The message MUST be a string or object implementing __toString(). * * The message MAY contain placeholders in the form: {foo} where foo * will be replaced by the context data in key "foo". * * The context array can contain arbitrary data, the only assumption that * can be made by implementors is that if an Exception instance is given * to produce a stack trace, it MUST be in a key named "exception". * * See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md * for the full interface specification. */ interface LoggerInterface { /** * System is unusable. * * @param string $message * @param array $context * @return void */ public function emergency($message, array $context = array()); /** * Action must be taken immediately. * * Example: Entire website down, database unavailable, etc. This should * trigger the SMS alerts and wake you up. * * @param string $message * @param array $context * @return void */ public function alert($message, array $context = array()); /** * Critical conditions. * * Example: Application component unavailable, unexpected exception. * * @param string $message * @param array $context * @return void */ public function critical($message, array $context = array()); /** * Runtime errors that do not require immediate action but should typically * be logged and monitored. * * @param string $message * @param array $context * @return void */ public function error($message, array $context = array()); /** * Exceptional occurrences that are not errors. * * Example: Use of deprecated APIs, poor use of an API, undesirable things * that are not necessarily wrong. * * @param string $message * @param array $context * @return void */ public function warning($message, array $context = array()); /** * Normal but significant events. * * @param string $message * @param array $context * @return void */ public function notice($message, array $context = array()); /** * Interesting events. * * Example: User logs in, SQL logs. * * @param string $message * @param array $context * @return void */ public function info($message, array $context = array()); /** * Detailed debug information. * * @param string $message * @param array $context * @return void */ public function debug($message, array $context = array()); /** * Logs with an arbitrary level. * * @param mixed $level * @param string $message * @param array $context * @return void */ public function log($level, $message, array $context = array()); }
关于message参数:
$message
必须是一个字符串或者是含有__toString()
方法的对象,$message
应该包含占位符,例如{placeholder_name}
,占位符由{
、占位符名称和}
组成,不能包含空格,占位符名称能够由A-Z, a-z, 0-9, _
组成,第三方实现能够用$context
参数来替换占位符,占位符名称必须和$context
数组的key对应。以下例子是使用$context
中的值替换$message
中的占位符:
/** * Interpolates context values into the message placeholders. */ function interpolate($message, array $context = array()) { // build a replacement array with braces around the context keys $replace = array(); foreach ($context as $key => $val) { // check that the value can be casted to string if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) { $replace['{' . $key . '}'] = $val; } } // interpolate replacement values into the message and return return strtr($message, $replace); } // a message with brace-delimited placeholder names $message = "User {username} created"; // a context array of placeholder names => replacement values $context = array('username' => 'Bolivar'); // echoes "User Bolivar created" echo interpolate($message, $context);
关于context参数:
$context
是一个数组参数,用于构造复杂的日志消息,$context
中的值不能跑出任何PHP异常或错误。若是$context
中包含Exception
对象,则该对象的key
必须为exception
。
Traits
。支持PSR-4自动加载器标准的PHP组建和框架,使用同一个自动加载器就能找到相关代码,而后将其载入PHP解释器。有了这个功能,就能够把现代PHP生态系统中不少客户操做的组件联系起来。
/** * 使用SPL组册这个自动加载函数后,遇到下述代码时这个函数会尝试 从/path/to/project/src/Baz/Qux.php文件中加载\Foo\Bar\Baz\Qux类: * new \Foo\Bar\Baz\Qux; * @param string $class 彻底限定的类名。 * @return void **/ spl_autoload_register(function ($class) { // 项目的命名空间前缀 $prefix = 'Foo\\Bar\\'; // 目录前缀对应的根目录 $base_dir = __DIR__ . '/src/'; // 判断传入的类是否使用了这个命名空间前缀 $len = strlen($prefix); if (strncmp($prefix, $class, $len) !== 0) { // 没有使用,交给注册的下一个自动加载器处理 return; } // 获取去掉前缀后的类名 $relative_class = substr($class, $len); // 把命名空间前缀替换成根目录, // 在去掉前缀的类名中,把命名空间分隔符替换成目录分隔符, // 而后在后面加上.php $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php'; // 若是该文件存在,就将其导入 if (file_exists($file)) { require $file; } });
先记着,后续再理解!