FIG-PHPPSR specification series 1-basic coding specifications

Source: Internet
Author: User
FIG-PHPPSR specification series 1-basic code specification 1. Introduction to FIG-PHP and PSR

The FIG-PHP has developed a series of PHP Development specifications, called the PSR, which is short for the Framework Interoperability Group, which is short for PHP Standard Recommendation. The FIG-PHP working group initially originated from the project representative's discussion of the commonalities between the two projects and found a way to work together. The main audience is the project team of both parties, but others in the PHP field are also watching. If others are willing to adopt the rules, you are welcome, but this is not the goal of the working group. No one in the working group will tell you how to build an application.

As of January 2015.07, a total of five standardized documents for the publication of The DSRs were as follows:

PSR-0 automatic loading (2014.10.21 abandoned, replacement specification for PSR-4)

Basic code specification for PSR-1

PSR-2 coding style

PSR-3 log interface

PSR-4 improved automatic loading

PSR-7 HTTP Message Interface


The following describes how to learn these specifications in multiple generations. This document is in series 1.

2. PSR-1: basic code specification

This specification discusses some basic code specifications for higher technical interoperability between different codes during code sharing in the future.

Required in this specification (MUST), NOT allowed (must not), SHOULD (shocould), should not (shocould NOT), can/MAY (MAY) for more information about the meanings of such keywords, see RFC 2119.

2.1 Overview

The source file MUST (MUST) be used only

The encoding format of the php code in the source file MUST (MUST) only use a UTF-8 without the byte sequence mark (BOM. Note: For more information about BOM, see [BOM].

We recommend that you only declare a source file (such as class, function, and constant), or perform auxiliary operations (such as output information and modification. ini configuration, etc.), but the two tasks should not be done at the same time.

The namespace and class must comply with one of the automatic load specifications of the DSRs: a PSR-0 or a PSR-4. Because the PSR-0 has been deprecated, it is actually equivalent to having to follow the PSR-4 specifications .)

The class name must be written in StudlyCaps.

(Translator's note: For more information, see [StudlyCaps]. StudlyCaps is a way to change uppercase and lowercase letters, some of which can be ignored. For example, messages may be hidden in uppercase and lowercase letters. for example, "ShoEboX" uppercase letters can be spelled out as "SEX", while "hobo" can be spelled out by lowercase grandmothers ", the webmail service provider Hotmail was initially written as HoTMaiL, and HTML can be spelled out with uppercase letters .)

Constants in a class must only consist of uppercase letters and underscores.

The method name must be in camelCase format.

2.2 files
2.2.1 PHP tag

PHP code must only use long labels ( ) Or short output label ( ), But cannot use other labels.

2.2.2 character encoding

The encoding format of PHP code must only use a UTF-8 without the byte sequence mark (BOM.

2.2.3 auxiliary functions

We recommend that you only declare a source file (such as class, function, and constant), or perform auxiliary operations (such as output information and modification. ini configuration), but it is not recommended to do both at the same time.

Side effects refers to some execution logics that are not directly related to declarations (classes, functions, constants, and so on.

In [PSR-1-translation 1], side effects are translated as side effects, and the translation in [PSR-1-translation 2] is a subordinate effect, here translation should be better for assisting ).

(Note: In the original article, merely from including the file is not clear about what it means, so it will be confirmed later ).

Auxiliary functions include but are not limited to: Generate outputs, explicitly use require or include to connect to external services, modify ini configurations, trigger errors or exceptions, and modify global or static variables, read or modify files.

The following is an example file that contains both declaration and auxiliary functions; that is, an example should be avoided:

 \ N "; // declare function foo () {// function body}


The following is an example file that only contains declarations:

 2.3 namespace and class name)

Namespace and class must follow an automatic loading specification PSR-0 or PSR-4.

This means that only one class is allowed in a source file, and at least one level is required in the namespace: a top-level organization name (vendor name ).

The class name must be written in StudlyCaps.

The code after PHP5.3 must use the formal namespace)

Example:

    


We recommend that you use the pseudo namespace Vendor _ as the prefix of the class name before PHP5.2.x.

     


2.4 constants, attributes, and methods of classes

A term class refers to all classes, interfaces, and trait.

2.4.1 constant

A class constant MUST be (MUST) composed of only uppercase letters and underscores. Example:

    2.4.2 attributes

Class attribute naming can follow a certain style in $ StulyCaps, $ camelCase or $ under_score. This specification is not mandatory. However, no matter which naming method is followed, it SHOULD be) in a certain range. This scope can be the entire team, the entire package, the entire class, or the entire method.

2.4.3 method

The method name MUST be (MUST) declared in the camelCase style.

3. References

[BOM] a blood case caused by a bom header,

[PHP-FIG] php-fig, http://www.php-fig.org/

PSR-1 basic code specification, PSR-1

PHP PSR-1 basic code specification, PSR-1

[PSR-4] FIG-PHP PSR specification series 4-automatic loading,

[RFC2119] Key words for use in RFCs to Indicate Requirement Levels, March 1997, http://www.ietf.org/rfc/rfc2119.txt

[RFC2119-Ruan Yifeng] RFC2119: indicates the required verb, http://www.ruanyifeng.com/blog/2007/03/rfc2119.html

[StudlyCaps] Studly caps, https://en.wikipedia.org/wiki/Studly_caps

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.