PHP-FIG PSR-1 # basic coding standard,
This standard includes the standard encoding elements used to ensure 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 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:
<? Php // marginal effect: Modify ini settings ini_set ('error _ report', E_ALL); // marginal effect: Load file include "file. php "; // marginal effect: generate output echo"
The following example only contains declarations with no marginal effect. It is worth following:
<? Php // declarationfunction foo () {// function body} // conditional declaration is not a marginal effect if (! Function_exists ('bar') {function bar () {// function body }}
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:
<?php// PHP 5.3 and later:namespace Vendor\Model;class Foo{}Code written for 5.2.x and before SHOULD use the pseudo-namespacing convention of Vendor_prefixes on class names.<?php// PHP 5.2.x and earlier:class Vendor_Model_Foo{}
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.
<?phpnamespace Vendor\Model;class Foo{ const VERSION = '1.0'; const DATE_APPROVED = '2012-06-01';}
Attribute This guide is intended to avoid bias StudlyCaps, Any attribute naming style in camelCase and $ under_score.
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 ().