PSR-1 basic code specification, psr-1 code specification

Source: Internet
Author: User

PSR-1 basic code specification, psr-1 code specification

This article provides standards for the basic elements of the Code to ensure that the shared PHP code has a high degree of technical interconnectivity.

Key words: "MUST" ("MUST"), "must not/must not" ("must not"), and "need" ("REQUIRED "), "will" ("SHALL"), "no" ("shall not"), "SHOULD" ("shocould"), and "shouldn't" ("shocould NOT ") for detailed descriptions of "Recommendations" ("RECOMMENDED"), "yes" ("MAY"), and "OPTIONAL" ("OPTIONAL"), see RFC 2119.

  • The PHP code file must start<?phpOr<?=Label Start;

  • The PHP code file must startUTF-8 without BOMEncoding;

  • In PHP code, only declarations such as classes, functions, and constants should be defined.Subordination(For example, generating file output and modifying the. ini configuration file), either of them can be selected;

  • The namespace and classes must comply with the automatic loading specification of the DPC: PSR-4;

  • The class name must followStudlyCapsUpper-case hump naming rules;

  • All the letters of a constant in the class must be uppercase, and words must be separated by underscores;

  • The method name must complycamelCaseType.

  1. File
2.1. PHP tag

PHP code is required<?php ?>Long tag or<?= ?>Short output tags; other custom tags are not allowed.

2.2. character encoding

PHP code is required and only usableUTF-8 without BOMEncoding.

2.3. subordination (side effects)

In a PHP file, you should define only new statements, such as operations that do not produce a subordinate effect, such as classes, functions, or constants, or only logical operations that produce a subordinate effect, but it should not have both.

The word "side effects" means logical operations performed only by including files, without directly declaring classes, functions, and constants.

The "subordinate effect" includes but is not limited to: generating output and directrequireOrinclude, Connect to external services, modify ini configurations, throw errors or exceptions, modify global or static variables, read or write files, etc.

The following is an example of an error that contains the Declaration and code that produces the subordination effect:

<? Php // subordination: Modify ini configuration ini_set ('error _ report', E_ALL); // subordination: Introduce file include "file. php "; // subordinate effect: generate output echo" 

The following is an example of a code that only contains declarations that do not produce a subordination effect:

<? Php // declare function foo () {// function subject part} // condition declaration ** not ** is a subordinate effect if (! Function_exists ('bar') {function bar () {// function subject }}
  1. Namespaces and Classes

The namespace and the class name must follow the PSR-4.

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

The class name must followStudlyCapsDescription: Description of the upper case.

PHP 5.3 and later versions of code must use a formal namespace.

For example:

<? Php // namespace Vendor \ Model; class Foo {} for PHP 5.3 and later versions {}

5.2.x and earlier versions should use the pseudo-namespace format. It is customary to use the top-level organization name (vendor name), as shown inVendor_Is the class prefix.

<? Php // 5.2.x and earlier versions written in class Vendor_Model_Foo {}
  1. Class constants, attributes, and methods

The "class" here refers to all classes, interfaces, and reusable code blocks (traits)

4.1. Constant

All the letters in the constants of the class must be uppercase and separated by dashes. Refer to the following code:

<?phpnamespace Vendor\Model;class Foo{    const VERSION = '1.0';    const DATE_APPROVED = '2012-06-01';}
4.2. Attributes

Class Attribute names can follow the upper-case hump ($StudlyCaps($camelCase) Or an underline separator ($under_score), This specification is not mandatory, but no matter which naming method is followed, it should 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 complycamelCase()Type.

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.