Home > Backend Development > PHP Tutorial > PHP PSR-1 basic code specification

PHP PSR-1 basic code specification

WBOY
Release: 2016-07-23 08:54:44
Original
973 people have browsed it
PHP, PSR
Basic code specifications

This specification sets out relevant standards for basic elements of code,
to ensure a high degree of technical interoperability between shared PHP codes.

Keywords "must" ("MUST"), "must not/must not" ("MUST NOT"), "need" ("REQUIRED"),
"will" ("SHALL"), "won't" ("SHALL NOT"), "should" ("SHOULD"), "should not" ("SHOULD NOT"),
"recommended" ("RECOMMENDED"), "can" ("MAY") and "optional" ("OPTIONAL") is described in detail in [RFC 2119][].

Overview


PHP code filesmuststart with

PHP code filesmustbe encoded in UTF-8 without BOM;

PHP codeshouldonly define declarations such as classes, functions, constants, etc., or other operations that will produce subordinate effects (such as generating file output and modifying .ini configuration files, etc.). You can only choose one of the two;

Namespaces and classesmustcomply with PSR's autoloading specification: one of PSR-0[];

The naming of classesmustfollow the StudlyCaps CamelCase naming convention starting with an uppercase letter;

All letters of the constants in the class must be in uppercase letters, and words should be separated by underscores;

The method name

must conform to the camelCase naming convention of starting with lowercase camelCase.

File

2.1. PHP tags
PHP code

mustuse long tag or short output tag; = ?>
must notuse other custom tags. 2.2. Character encoding

PHP code

mustand can only use UTF-8 encoding without BOM. 2.3. Subordinate effects (side effects)

In a PHP file,

should either only define new declarations, such as classes, functions or constants, and other operations that do not produce dependency effects, or only logical operations that will produce dependency effects, but should not both at the same time Has both.

The term "side effects" means logical operations performed only by including files without directly declaring classes,

functions, constants, etc.

"Dependent effects" include but are not limited to: generating output, direct require or include, connecting external services, modifying ini configuration, throwing errors or exceptions, modifying global or static variables, reading or writing files, etc.

The following is a counter-example, a code that contains declarations and produces dependency effects:

    // Dependency effects: modify the ini configuration
  1. ini_set('error_reporting', E_ALL);
  2. // Dependency Effect: Introduce file
  3. include "file.php";
  4. // Dependent effect: Generate output
  5. echo "n";
  6. // Declare function
  7. function foo()
  8. {
  9. // Function body
  10. }
Copy code
The following is an example, a code that only contains declarations that do not produce dependency effects:

    // Declare function
  1. function foo()
  2. {
  3. / / Function body
  4. }
  5. // Conditional declarations are **not** subordinate effects
  6. if (! function_exists('bar')) {
  7. function bar()
  8. {
  9. // Function body
  10. }
  11. }
Copy code
Namespaces and classes


Namespace and class naming must follow [PSR-0][].

According to the specification, each class is an independent file, and the namespace has at least one level: the top-level organization name (vendor name).

Class naming must follow the StudlyCaps camel case naming convention starting with an uppercase letter.

Code for PHP 5.3 and later versions

mustuse the official namespace.

For example:

    // How to write PHP 5.3 and later versions
  1. namespace VendorModel;
  2. class Foo
  3. {
  4. }
Copy code
5.2.x and previous versions

You should use pseudo namespace writing, and the convention is to use the top-level organization name (vendor name) such as Vendor_ as the class prefix.

// How to write 5.2.x and previous versions
  • class Vendor_Model_Foo
  • {
  • }
  • Copy code
  • Constants, properties and methods of the class

    "Class" here refers to all classes, interfaces and reusable code blocks (traits)
    4.1. Constants All letters in

    class constants

    must be

    capitalized, and words are separated by underscores. Refer to the following code:

    namespace VendorModel;
  • class Foo
  • {
  • const VERSION = '1.0';
  • const DATE_APPROVED = '2012-06-01';
  • }
  • Copy Code
  • 4.2. Properties

    The attribute naming of the class can follow the camel case starting with upper case ($StudlyCaps), the camel case starting with lower case ($camelCase), or the underline delimited format ($under_score). This specification does not make it mandatory, but no matter which naming is followed Methods should all be consistent within a certain range. This scope can be the entire team, the entire package, the entire class, or the entire method. 4.3. Method The method name

    must

    conform to the camelCase()-style camelCase naming convention starting with lowercase. Reposted from Github(PizzaLiu)

    Related labels:
    source:php.cn
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template