zoukankan      html  css  js  c++  java
  • PSR

    PSR是PHP Standards Recommendation的简称,这个是php-fig组织制定的一套规范

    PSR-1

    PHP标签:
    PHP代码必须放在<?php ?>标签或<?= ?>标签中。

    编码:
    PHP文件必须使用无BOMUTF-8编码。

    副作用:
    一个PHP文件可以定义符号(比如类、函数、常量等),或者执行只有唯一副作用的操作(比如输出结果、处理数据等),但是不能同时做这两件事,尽量是一个PHP文件的功能单一。在操作的时候尽量把变量、类、函数的声明分开,通过includerequire文件的方式来使用。(一个PHP文件一个作用)

    类的名称:
    每个类都有自己的命名空间,且都在顶级命名空间下,类名必须使用驼峰式 CamelCase

    常量:

    常量必须全部是用大写,并且使用下划线(_)分开
    类的方法:
    类的方法必须使用小写字母开头的驼峰式(camelCase)命名
    PSR-2
    PHP文件必须使用Unix风格的换行符(LF, linefeed),最后要有一个空行,每行代码不应该超过80个字符,每行末尾不能有空格,每行只能有一条语句,可以在适当的地方添加空行提高代码的阅读性
    不加上?>关闭标签,可以避免意料之外的输出错误,如果加上关闭标签,且在关闭标签后有空行,那么空行会被当成输出,导致意想不到的错误。
    缩进:
    必须以4个空格为缩进,不能使用制表符(Tab键)缩进。
    关键字:
    PHP的关键字必须使用小写,而且truefalse, 和 null也必须小写
    命名空间和use声明:
    现在,namespace声明之后必须要有一个空行,而且use声明必须放在namespace之后,必须分别使用use引入命名空间,而且use后要有空行
    namespace VendorPackage;
    
    use FooClass;
    use BarClass as Bar;
    use OtherVendorOtherPackageBazClass;
    
    // ... additional PHP code ...

    类的继承和实现:
    extendsimplements关键字必须和类名在同一行,类、接口和Traits定义体的起始括号应该在类名之后新起一行,结束括号也必须新起一行,例如

    namespace VendorPackage;
    
    use FooClass;
    use BarClass as Bar;
    use OtherVendorOtherPackageBazClass;
    
    class ClassName extends ParentClass implements ArrayAccess, Countable
    {
        // constants, properties, methods
    }

    如果implements后面后很多类导致一行很长,可以依次将需要的类另起新行并缩进4个空格

    namespace VendorPackage;
    
    use FooClass;
    use BarClass as Bar;
    use OtherVendorOtherPackageBazClass;
    
    class ClassName extends ParentClass implements
        ArrayAccess,
        Countable,
        Serializable
    {
        // constants, properties, methods
    }

    可见性:
    类中的每个属性和方法都要声明可见性,有publicprivateprotected,不能使用var关键词来声明,老版本的PHP会在私有属性前加上_,一行只能声明一个属性

     方法:
    类中的所有方法也应该定义可见性,方法名后面不能有空格,方法体的括号位置和类定义体的括号位置一样,都要新起一行,结束括号也要新起一行。方法参数的起始圆括号之后没有空格,结束括号之前也没有空格,有多个参数是,每个参数的逗号后面加一个空格,例如:

    namespace VendorPackage;
    
    class ClassName
    {
        public function fooBarBaz($arg1, &$arg2, $arg3 = [])
        {
            // method body
        }
    }

    如果参数比较多,需要换行时,可以如下

    namespace VendorPackage;
    
    class ClassName
    {
        public function aVeryLongMethodName(
            ClassTypeHint $arg1,
            &$arg2,
            array $arg3 = []
        ) {
            // method body
        }
    }

    abstractfinalstatic
    现在,abstractfinal必须在可见性修饰符之前,static声明必须放在可见性修饰符之后,例如:

    namespace VendorPackage;
    
    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
    );
    PHP的控制结构:
    PHP的控制结构包括if、else、elseif、switch、case、while、do while、for、foreach、try和catch。如果这些关键词后面有一对原括号,开始括号前必须有一个空格,与方法和类的定义体不同,控制结构关键词后面的起始括号应该和控制结构关键词写在同一行,例如
    $gorilla = new AnimalsGorilla;
    $ibis = new AnimalsStrawNeckedIbis;
    
    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 PsrLog;
    
    /**
     * 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

    PSR-4
    PSR-4规范描述了一个标准的自动加载器策略,指在运行时按需查找PHP类、接口或Traits。支持PSR-4自动加载器标准的PHP组建和框架,使用同一个自动加载器就能找到相关代码,然后将其载入PHP解释器。有了这个功能,就可以把现代PHP生态系统中很多客户操作的组件联系起来。
    /**
     * 使用SPL组册这个自动加载函数后,遇到下述代码时这个函数会尝试   从/path/to/project/src/Baz/Qux.php文件中加载FooBarBazQux类:
     *  new FooBarBazQux;
     * @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;
        }
    });

    先记着,后续再理解!



    原文地址:https://www.jianshu.com/p/b33155c15343

  • 相关阅读:
    一些业内有名的网站收集
    WCF重载
    FCKEditor fckconfig.js配置,添加字体和大小 附:中文字体乱码问题解决
    查询第几条到第几条的数据的SQL语句
    SPOJ 9939 Eliminate the Conflict
    UVA 10534 Wavio Sequence
    HDU 3474 Necklace
    POJ 2823 Sliding Window
    UVA 437 The Tower of Babylon
    UVA 825 Walking on the Safe Side
  • 原文地址:https://www.cnblogs.com/ValleyUp/p/11181390.html
Copyright © 2011-2022 走看看