PHP Standard Specification Detailed

Source: Internet
Author: User
Tags autoloader naming convention
This article mainly and everyone to share the PHP standard specification detailed, hope to help everyone.

PSR0: Automatic loading standard

has been replaced by PSR4, can understand the next

1. Mandatory: Fully qualified namespace and class format: \<vendor name>\ (<namespace>\) *<class name>
2. Mandatory: Each namespace must have a top-level namespace (Vendor name).
3. Each namespace can contain any number of sub-namespaces.
4. When the file system loads the source file, the delimiter between the namespaces is converted to Directory_separator.
5. The underscore _ in class name <class name> will be converted to Directory_separator. _ There is no special meaning.
6. Fully qualified namespaces and classes are appended with the. php suffix when loading source files from the file system.
7. The letter case in the fully qualified namespace can be any combination.

Instance

\doctrine\common\isolatedclassloader =/path/to/project/lib/vendor/doctrine/common/isolatedclassloader.php\ Symfony\core\request =/path/to/project/lib/vendor/symfony/core/request.php\zend\acl =/path/to/project/ Lib/vendor/zend/acl.php\zend\mail\message =/path/to/project/lib/vendor/zend/mail/message.php\namespace\ Package\class_name =/path/to/project/lib/vendor/namespace/package/class/name.php\namespace\package_name\ Class_name =/path/to/project/lib/vendor/namespace/package_name/class/name.php

PSR1: BASIC Programming specification

Mandatory: PHP tag must be <?php?> or <?=? >2. Mandatory: The file must be encoded with UTF-8 without BOM 3. The code in a PHP file can only be declarations that define classes, functions, constants, or only vice-operation 4. Namespaces and classes must meet Psr0 or psr45. The class name must be "Hump naming" Specification 6, StudlyCaps "uppercase." All the letters in a class must be uppercase and the underscore delimited by 7. The method name must be the CamelCase hump naming convention.

Explain

Sub-operations include, but are not limited to, generating output, direct require or include, connecting to external services, modifying INI configuration, throwing errors or exceptions, modifying global or static variables, reading or writing files, and so on. <?php//side effect:change Ini settingsini_set (' error_reporting ', e_all);//Side effect:loads a Fileinclude "file.php ";//Side effect:generates Outputecho" 

PSR2 Programming Style Specification

Is the expansion of PSR1

1. Psr12 must be followed. Indentation must use 4 space characters 3. The number of characters per line should remain soft within 80, theoretically must not be more than 120, but must not have a hard limit. 4. After each namespace declaration statement and use declaration statement block, you must insert a blank line. All use must be declared after namespace. 5. The opening curly braces of methods and classes must be written in a separate row after their declaration. 6. Abstract and final must precede the modifier private, protected, and public, and static must be declared after the access modifier. 7. Control the structure of the keyword must have a space character, and call the method or function must not have. 8. The opening curly brace of the control structure must be written on the same line of the declaration. 9. The start of the control structure must not have any whitespace before the opening parenthesis and the closing parenthesis. 10. All PHP files must use the Unix LF as the line terminator.
11. All PHP files must end with a blank line. 12. There must be no extra whitespace after the non-empty line. 13. There must be more than one statement per line. All keywords in PHP must be all lowercase.

Instance

<?phpnamespace 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 ($arg 1);        } else {            Bazclass::bar ($arg 2, $arg 3);        }    }    Final public static function bar ()    {        //method body    }}

PSR4 Automatic loading

\<namespacename> (\<subnamespacenames>) *\<classname>
Note: The PSR4 is no longer called a fully qualified namespace, but rather a fully qualified class name. 1. The fully qualified class name must have a top-level namespace, called "Vendor namespace" 2. A fully qualified class name can have one or more sub-namespaces 3. The complete class name must have a final class name; 4. Underscores are 5 with no special meaning. The complete class name can consist of any uppercase and lowercase letters; 6. All class names must be case-sensitive. 7. In the complete class name, remove the first namespace delimiter, one or more contiguous namespaces and sub-namespaces as "namespace prefixes", which must correspond to at least one "file base directory"; 8. The child namespace immediately following the namespace prefix must match the corresponding file base directory, where the namespace delimiter is used as the directory delimiter. 9. The class name at the end must have the same name as the corresponding file with the. php suffix. 10. The implementation of the autoloader (autoloader) must not throw an exception, must not trigger any level of error messages, and should not have a return value.

Instance

namespace prefix file base directory file path
\acme\log\writer\file_writer acme\log\writer ./acme-log-writer/lib/ ./acme-log-writer/lib/file_writer.php
\aura\web\res Ponse\status aura\web /path/to/aura-web/src/ /path/to/aura-web/src/response/status.php
\symfony\core\request symfony\core ./vendor/symfony/core/ ./vendor/symfony/core/request.php
\zend\acl Zend /usr/includes/zend/ /usr/includes/zend/acl.php

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.