PHP-FIGPSR-1 # Basic Coding Standards

Source: Internet
Author: User
Tags coding standards
PHP-FIGPSR-1 # basic coding standards this standard contains a standard encoding element for ensuring the interoperability of PHP shared code.

The key words "MUST", "must not", "REQUIRED needs", "SHALL will", "shall not will NOT", "shoshould ", "shoshould NOT", "RECOMMENDED by RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

Overview
  1. File must use <? Php and <? = Tag.
  2. The PHP code in the file must be encoded using a non-BOM header UTF-8.
  3. Files can declare "symbols" (such as classes, functions, constants, and so on), and can also perform marginal effects (such as generating output and changing ini settings .). But these two types of things should not appear in the same file.
  4. The namespace and class must follow the "autoloading" PSR: [PSR-0, PSR-4].
  5. The class name must be declared using StudlyCaps (upper-case camper.
  6. Class constants must use uppercase letters separated by underscores.
  7. The method name must use the hump declaration.
File PHP tag

PHP code must use long labels <? Php?> Or echo short tag <? =? >; Other labels must not be used.

Character encoding

PHP code must be encoded using a UTF-8 without BOM header

Marginal effect

A file (should) either declares symbols (classes, functions, constants, etc.), and cannot cause any marginal effect; or executes code that produces marginal effects. These two cases cannot appear in a file.
The "marginal effect" executes logics unrelated to declarative classes, functions, and constants.

Marginal effects include but are not limited to: generating output, explicit require or include, connecting to external services, modifying ini configurations, issuing errors or exceptions, and modifying global or static variables, read and write files.

The following is an example of declarative symbols that produce marginal effects. This should be avoided:

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

The following example only contains declarations with no marginal effect. it is worth following:

 Namespace name and class name

The namespace name and class name must follow the "autoloading" PSR: [PSR-0, PSR-4].

This means that each file can only contain one class and has at least one namespace: top-level vendor name.

The class name must be declared using StudlyCaps (upper-case camper.

For PHP 5.3 and later versions, you must use a formal namespace (before PHP 5.3, namespace is not supported ).

For example:

  Class constants, attributes, and methods

The "classes" here include classes, interfaces, and traits.

4.1. constant

Class constants must be declared in uppercase using underscores.

 
   Attribute

This guide avoids the preference of StudlyCaps, camelCase, and $ under_score for any attribute naming style.

No matter which naming style is used, it should be consistent within a reasonable range. The range can be set to "vendor-level", "package-level", "class-level", or "method-level.

Method

CamelCase () must be used for method names ().

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.