fig-php PSR Specification Series 1-Basic coding specification

Source: Internet
Author: User
Tags rfc

1. Introduction to fig-php and PSR

Fig-php developed a series of PHP development specifications, referred to as PSR, here is the Framework interoperability Workgroup (Framework interoperability Group) abbreviation, PSR is the PHP standard recommendation (PHP Standard recommendation) abbreviation. The fig-php Working Group was originally based on a project representative's discussion of the commonalities between two projects to find ways to work together. The main audience is the project team, but other people in the PHP community are watching. If others are willing to adopt the norms here, then welcome, but this is not the goal of the Working Group. No one in the workgroup will tell you how to build the app.

As of 2015.07, PSR has a total of five specification documents published, namely:

  • PSR-0 Automatic loading (2014.10.21 obsolete, replacement specification for PSR-4)

  • PSR-1 Basic Coding Specification

  • PSR-2 Coding Style

  • PSR-3 Log Interface

  • PSR-4 Improved auto-loading

  • PSR-7 HTTP Message interface


These specifications will be learned in multiple series below, this document is series 1.

2. PSR-1: Basic Coding specification

    This specification discusses some basic code specifications for future code sharing between different code can have a higher degree of technical interoperability.

This specification must be (must), not (must not), should (should), should not (should not), can/might (may) and other keywords meaning can be see RFC 2119.

2.1 Overview
  • The source file must (must) use only the <?php and <?= labels.

  • The encoding format of the PHP code in the source file must (must) use only UTF-8 without a byte order mark (BOM). Note: see resources [BOM] for more BOM information.

  • A source file is recommended only for declarations (classes (class), Functions, constants (constant), etc.) or only for auxiliary operations (for example, output information, modify. ini configuration, etc.), but should not be (should not ) do both things at the same time.

  • Namespaces (namespace) and classes (class) must follow a PSR auto-load specification: PSR-0 or PSR-4. ( Translator Note : Since PSR-0 has been discarded, this is actually equivalent to having to follow the PSR-4 specification .) )

  • Class name must use StudlyCaps notation.

    (Translator Note: see Resources [StudlyCaps], studlycaps is an arbitrary case of uppercase and lowercase letters, some of which can be ignored as a notation.) For example, messages may be hidden in uppercase and lowercase letters, such as "shoebox" uppercase letters can be spelled "SEX", lowercase grandmother can spell "hobo", webmail service Hotmail was originally written as Hotmail, capital letters can be spelled out HTML. )

  • Constants in class must consist only of uppercase letters and underscores (_).

  • The method name must be written using the CamelCase ( camel-style) notation.

2.2 Files
2.2.1 PHP Tags

The PHP code must use only long labels (<?php?>) or short output labels (<?=?>), but not other tags.

2.2.2 character encoding

the encoding format of the PHP code must use only UTF-8 with no byte order mark (BOM).

2.2.3 Auxiliary function

A source file is recommended only for declarations (classes (class), Functions, constants (constant), etc.) or only for auxiliary operations (for example, output information, modify. ini configuration, etc.), but it is not recommended to do both.

phrase assist (side effects) means Some execution logic that is not directly related to declaring (class), Functions (function), constants (constant), and so on.

    (Translator note: [psr-1-translation 1] side effects translated as side effects ,[psr-1-Translation 2] translated as subordinate effect &NBSP, all feel unintelligible, here translates as auxiliary function should be better ).

(Translator Note: the original merely from including the file is not clear what the meaning, left to confirm later).

Ancillary actions include, but are not limited to: generating output, explicitly using require or include, connecting external services, modifying INI configuration, triggering errors or exceptions, modifying global or static variables, reading or modifying files, and so on .

The following is an example file that contains both a declaration and an auxiliary function, which should be avoided:

<?php//side effects: Modified INI configuration ini_set (' error_reporting ', e_all);//Side effects: Loaded file include "file.php";//Side effects: Produced output echo "


The following is an example file that contains only declarations, which should be advocated:

<?php//declaration function foo () {///Body}//Conditional declaration does not count as auxiliary if (! function_exists (' Bar ')) {function bar ()} {///functions Body}}
2.3 Space Name (namespace) and class name

Namespaces (namespace) and classes (class) must follow an automatic loading specification PSR-0 or PSR-4.

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

class name must use StudlyCaps notation.

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

Example:

<?php//PHP 5.3 and later: namespace Vendor\model;class foo{}


php5.2.x code suggested using pseudo-namespace Vendor_ as the class name prefix

<?php//php 5.2.x and before: Class vendor_model_foo{}


constants, properties, and methods for class 2.4

the term class refers to all classes (class), interfaces (interface), and trait.

2.4.1 Constants

class constants must (must) consist only of uppercase letters and underscores (_). Example:

<?phpnamespace vendor\model;class foo{Const VERSION = ' 1.0 '; Const date_approved = ' 2012-06-01 ';}
2.4.2 Property

Class Property naming can follow $stulycaps, $camelCase or $under_score in a style, this specification does not make mandatory requirements, but regardless of which naming method, should be (should) within a certain scope of consistency. This range can be the entire team, the entire package, the entire class, or the entire method.

2.4.3 Method

The method name must (must) be declared using the CamelCase style.

3. References

[BOM] A BOM head of a murder, http://blog.csdn.net/ohmygirl/article/details/6931716

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

[ psr-1-translation 1 ] PSR-1 Basic code specification,   https://github.com/hfcorriez/fig-standards/blob/zh_CN/%E6%8E%A5%E5%8F%97/ PSR-1-BASIC-CODING-STANDARD.MD

[psr-1-translation 2]  PHP PSR-1 Basic Code Specification (Chinese version),  http://segmentfault.com/a/1190000002521577

[PSR-4] fig-php PSR Specification series 4-Auto load, http://my.oschina.net/1pei/blog/485099

[RFC2119] Key words for use with RFCs to indicate requirement levels, March 1997, Http://www.ietf.org/rfc/rfc2119.txt

[rfc2119-Ruan Yi Feng] RFC2119: The verb that represents the request, http://www.ruanyifeng.com/blog/2007/03/rfc2119.html

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

fig-php PSR Specification Series 1-Basic coding specification

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.