What are the PSR specifications in the PHP API framework

03-13-2023

This article mainly introduces the relevant knowledge of the PSR specification in the PHP API framework. The editor shows you the operation process through actual cases. The operation method is simple, fast, and practical. I hope that the PSR specification in the PHP API framework has some Which articles can help you solve the problem.


What is PSR?

Before introducing PSR, you need to introduce the organization that made this specification -----PHP-FIG, the full name is PHP Framework Interoperability.

Organization members formulate specifications and implement them in their own projects. Although they are not official organizations, they also represent most of the PHP community projects, such as CakePHP, Composer, Drupal, PEAR, Slim, Yii framework, Zend Framework and so on. And there are more and more projects joining and following this standard.

The purpose of the PSR project is to formulate a collaboration standard with minimal restrictions through discussions between framework authors or framework representatives. Each framework follows a unified coding standard, allowing engineers to work together better .

Up to now, 20 items have been listed on the official website, excluding those that are being drafted and discarded, there are the following 13 items.

QQ截图20230306161429.jpg

Let's take a look at these specifications:

PSR-1 Basic Coding Specification

  • PHP code file must start with <?php or <?= tag
  • PHP code file must be encoded in UTF-8 without BOM

  • PHP code should only define declarations such as classes, functions, constants, or other operations that will produce side effects (such as: generating file output and modifying . Choose one

  • The namespace and class must conform to the PSR's autoloading specification PSR-4

  • The naming of the class must follow StudlyCaps-style camel case naming convention

  • All letters of the constants in the class must be capitalized, and words are separated by underscores

  • The method name must conform to the camelCase-style camelcase naming convention

PSR-12 code style specification

PSR- The specification of 12 is very detailed, including declarations, namespaces, classes and inheritance, and control structures.

Let's take a look at a demo first:

<?phpdeclare(strict_types=1);namespace Vendor\Package;use Vendor\Package\{ClassA as A, ClassB, ClassC as C};use Vendor\Package\SomeNamespace\ClassD as D;use function Vendor\Package\{functionA, functionB, functionC};use const Vendor\Package\{ConstantA, ConstantB, ConstantC};class Foo extends Bar implements FooInterface{
    public function sampleFunction(int $a, int $b = null): array    {
        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 the coding standards in [PSR-1]

  • All PHP Files must use Unix LF (linefeed) as line terminators;

  • All PHP files must end with a blank line;

  • Pure PHP code files must omit the last ?> end tag

  • The number of characters in each line should be softly kept within 80, and theoretically must not be more than 120, but there must be no hard limit; there must be no extra spaces after non-blank lines;

  • Blank lines can make reading the code more convenient and help the code Block.

  • There must not be more than one statement in each line

  • The code must be indented with 4 spaces, and the tab key must not be used

  • All keywords in PHP must be all lowercase

  • The basic specification of the control structure is as follows:


There must be a space after the control structure keyword. There must be no spaces after the opening bracket ( . There must also be no spaces before the closing parenthesis ) . There must be a space between the closing brace ) and the opening curly brace { . The body of the structure must be indented once. The closing curly brace } must be on a separate line after the body of the structure. Copy code


There are many code style specifications, so I won’t explain them one by one here, you can read them yourself

PSR-4 autoload

Regarding the relevant specifications of automatically loading corresponding classes from file paths, this specification is interoperable and can be used as a supplement to any automatic loading specification, including PSR-0, in addition , this PSR also includes the file storage path specification corresponding to the automatically loaded class.

  • The class here refers to all classes, interfaces, traits, reusable code blocks, and other similar structures.

  • A complete class name needs to have the following structure:


    \(\)*\


    • The full class name must have a top-level namespace called the "vendor namespace";

    • The full class name can< /strong>has one or more subnamespaces;

    • full class name must have a final class name;

    • The underscore in any part of the complete class name has no special meaning;

    • The complete class namecanConsists of any uppercase or lowercase letters;

    • All class names must be case sensitive.

  • When loading the corresponding file according to the complete class name...

    • Complete In the class name, remove the leading namespace separator, and connect the precedingOne or more namespaces and subnamespaces following the namespace prefix, which must correspond to at least one file base directory;

    • The subname immediately after the namespace prefix The namespace must match the corresponding file base directory, where the namespace separator will be used as the directory separator.

    • The class name at the end must have the same name as the corresponding file suffixed with .php.

    • Autoloader implementations must not throw exceptions, must not trigger errors of any level information and should not have a return value.

Example

The following table shows the full class name, namespace prefix and The file path corresponding to the file base directory.

full class namenamespace prefixfile base directoryfile path
\Acme\Log\Writer\File_WriterAcme\Log\Writer./acme- log-writer/lib/./acme-log-writer/lib/File_Writer.php
\Aura\Web\Response\StatusAura\Web/path/to/aura-web/src//path/to/aura-web/src/Response/Status.php< /td>
\Symfony\Core\RequestSymfony\Core./vendor/Symfony/Core/./vendor/Symfony/Core/Request.php
\Zend\AclZend/usr/includes/ Zend//usr/inincludes/Zend/Acl.php

For the time being, we will only introduce the three commonly used specifications.


Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us