Basic code specification

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

Key words “MUST” (” MUST “), “MUST NOT/mustn’t” (” MUST NOT “), “needs” (” REQUIRED “), “will” (” SHALL “) and “no” (” SHALL NOT “), “SHOULD”, “SHOULD”, “shouldn’t” (” SHOULD NOT”), “RECOMMENDED”, “MAY”, and “OPTIONAL” are detailed in RFC 2119.

  1. An overview of

  • PHP code files must start with

  • PHP code files must be encoded in UTF-8 without BOM;

  • PHP code should only define class, function, constant declarations, or any other actions that have a dependency effect (e.g., generating file output or modifying.ini configuration files).

  • Namespaces and classes must conform to the PSR autoload specification: PSR-4;

  • Class names must follow the hump naming convention beginning with StudlyCaps.

  • Constants in a class must be capitalized and words separated by underscores;

  • Method names must conform to the Camelcase-style camel naming convention with a lowercase beginning.

  1. file

2.1. The PHP tags

PHP code must use
long tag or
short output tag; Do not use other custom tags.

2.2. Character encoding

PHP code must and can only use UTF-8 encoding without BOM.

2.3. Subordinate Effects (Side effects)

A PHP file should either define new declarations such as classes, functions, or constants that do not have dependencies, or logical operations that do, but not both.

The term “side effects” refers to a logical operation performed simply by including files without directly declaring classes, functions, constants, etc.

Dependency effects include but are not limited to generating output, direct require or include, connecting to external services, modifying INI configurations, throwing errors or exceptions, modifying global or static variables, reading or writing files, etc.

Here is an example of an error, a code containing declarations and subordination effects:


      
// Subordinate effect: Modify ini configuration
ini_set('error_reporting', E_ALL);

// Dependency effects: import files
include "file.php";

// Slave effect: generates output
echo "<html>\n";

// Declare the function
function foo(a)
{
    // The body of the function
}
Copy the code

Here is an example of a code containing only a declaration that does not have a dependency effect:


      
// Declare the function
function foo(a)
{
    // The body of the function
}

// Conditional declaration ** does not ** belong to subordinate effect
if (! function_exists('bar')) {
    function bar(a)
    {
        // The body of the function}}Copy the code
  1. Namespaces and classes

The naming of namespaces and classes must follow PSR-4.

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

Class names must follow the hump naming convention beginning with StudlyCaps.

PHP 5.3 and later code must use an official namespace.

Such as:


      
// PHP 5.3 and later
namespace Vendor\Model;

class Foo
{}Copy the code

5.2.x and earlier versions should be written in pseudo-namespaces, with the convention of using a top-level organization name such as Vendor_ as a class prefix.


      
// 5.2.x and previous versions
class Vendor_Model_Foo
{}Copy the code
  1. Class constants, properties, and methods

Classes refer to all classes, interfaces, and reusable code blocks (traits).

4.1. The constant

All letters in class constants must be capitalized, with words separated by an underscore. Refer to the following code:


      
namespace Vendor\Model;

class Foo
{
    const VERSION = '1.0';
    const DATE_APPROVED = '2012-06-01';
}
Copy the code

4.2. The attribute

Class attributes can be named in uppercase camelCase ($StudlyCaps), lowercase camelCase ($camelCase), or underscore separated ($under_score). This specification is not mandatory, but it should be consistent to a certain extent regardless of which naming method is followed. This scope can be a whole team, a whole package, a whole class, or a whole method.

Method of 4.3.

Method names must conform to the camelCase() lowercase hump naming convention.


Original text: github.com/PizzaLiu/PH…