Before you introduce PSR-0, let's talk about namespaces (NameSpace) and autoloading.NameSpace (namespace)Namespace is a new feature of the PHP5.3 release that addresses two types of problems encountered when writing a class library or application to create reusable code such as classes or functions:1. User-written code conflicts with the name of a class/function/constant or third-party class/function/constant within PHP.2. Create an alias (or short) n
: This article mainly introduces the PSR-4 instance, for PHP tutorials interested in students can refer. Here is an example of implementing the PSR-4 specification:
Closure Example
Class Example
The following is an example of a class for processing multiple namespaces.
Register (); ** // register the base Directory for the namespace prefix. * $ Loader-> addNamespace ('foo \ bar', '/path/to/packages
1. PSR-0 specification[1] namespace must be consistent with absolute path[2] class name first letter must be capitalized[3] Except for the entry file, the other ". PHP" must have only one class[4]php class files must be loaded automatically, not include, etc.[5] Single inlet2. Case[1] directory structure[2] Source codeindex.php' Basedie ', __dir____); Require_once ('/config/loader.php '); Spl_autoload (' \\config\\loader.php::autoload '); Config\obje
Generally speaking, our PSR-0 specification can generally be divided into the following five parts:(1) Class name space must be consistent with absolute path(2) The first letter of the class name must be capitalized(3) Except for the entry file, the other PHP files must have only one class(4) PHP files must be able to be loaded automatically, cannot have include and so on loading(5) Single entryCopyright NOTICE: This article for Bo Master original art
This standard contains standard coding elements that are used to ensure interoperability of PHP shared code.The key words "must must", "must not must not", "REQUIRED required", "shall will", "shall not will not", "should should", "should not should not", "Recomme NDED recommended "," may can ", and" OPTIONAL optional "in this document is to is interpreted as described in RFC 2119.Overview
File must use
The PHP code in the file must be UTF-8 encoded with no BOM header.
A file ca
This part of the standard includes what should be considered standard coding elements to ensure a high degree of technical interoperability between shared PHP code.Keywords "must" ("must"), "must not/must not be" ("must not"), "Need" ("REQUIRED"),"Will" ("Shall"), "no" ("Shall not"), "should" ("should"), "shouldn't" ("should not"),Detailed descriptions of "recommended" ("RECOMMENDED"), "Can" ("may") and "optional" ("OPTIONAL") can be found in RFC 21191. Overview
PHP files must be used o
This standard contains standard coding elements that are used to ensure interoperability of PHP shared code.
The key words "must must", "must not must not", "REQUIRED required", "shall will", "shall not will not", "should should", "should not should not", "Recomme NDED recommended "," may can ", and" OPTIONAL optional "in this document is to is interpreted as described in RFC 2119.Overview
File must use
The PHP code in the file must be UTF-8 encoded with no BOM header.
A file can
Basic code SpecificationThis specification formulates the relevant standards for the basic elements of Code,To ensure a high degree of technical interoperability between shared PHP code.Keywords "must" ("must"), "must not/must not be" ("must not"), "Need" ("REQUIRED"),"Will" ("Shall"), "no" ("Shall not"), "should" ("should"), "shouldn't" ("should not"),Detailed descriptions of "recommended" ("RECOMMENDED"), "Can" ("may") and "optional" ("OPTIONAL") can be found in RFC 2119.1. Overview
P
PHP PSR-1 Basic Code Specification (Chinese version)
Basic code Specification
This specification formulates the relevant standards for the basic elements of Code,To ensure a high degree of technical interoperability between shared PHP code.
Keywords "must" ("must"), "must not/must not be" ("must not"), "Need" ("REQUIRED"),"Will" ("Shall"), "no" ("Shall not"), "should" ("should"), "shouldn't" ("should not"),Detailed descriptions of "recommended" ("REC
1. Overview1. 必须使用2. File 2.1PHP TagPHP code must use 2.2 Character encodingPHP code must be encoded using UTF-8 without BOM2.3 Contents of the fileThe rule suggests that a file should have only one behavior, such as the definition of the interface to do the method, the specific function implemented in the implementation class, and the recommendation not to write the interface and implementation in the same file This is life and the use of mixed writing, the rules do not recommend this writing,
1. The code must be indented using 4 space characters instead of the TAB key.2. After each namespace namespace declaration statement and the use declaration statement block, you must insert a blank line.3. The opening curly brace ({) of a class must be written in a line after the function declaration, and the closing curly brace (}) must also be written in a row after the function body.4. The opening curly brace ({) of the method must be written in a line after the function declaration, and the
PSR-1 basic 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.
1. Overview -----------
PHP code fileRequiredTo
PHP code fileRequiredIt is encoded with a UTF-8 without BOM;
PHP codeShouldOnly declarations such as classes, functions, and constants are defined, or other operations that will produce a subordinate effect
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 Character encoding
This specification is not many, seven points are as follows:1. Overview
Files must use only and tags.
Files must use is UTF-8 without BOM for PHP code.
Files should either declare symbols (classes, functions, constants, etc.) orcause side-effects (e.g. generate output, change. ini settings, etc.) but should does both.
Namespaces and classes must follow an "autoloading" PSR: [PSR-0,
Basic code SpecificationThis specification formulates the relevant standards for the basic elements of Code,To ensure a high degree of technical interoperability between shared PHP code.Keywords "must" ("must"), "must not/must not be" ("must not"), "Need" ("REQUIRED"),"Will" ("Shall"), "no" ("Shall not"), "should" ("should"), "shouldn't" ("should not"),A detailed description of "recommended" ("RECOMMENDED"), "Can" ("may") and "optional" ("OPTIONAL") can be found in [RFC 2119][].OverviewThe PHP c
. IF Middleware is * registered for the route, each callable middleware are invoked in * the order specified. * * @param serverrequestinterface $request The current request object * @param responseinterface $response T He current Response object * @return \psr\http\message\responseinterface * @throws \exception if the route Callab Le throws an exception * * Public function __invoke (serverrequestinterface $request, Responseinterface $response)
This article introduces the PHP design pattern one of the namespace, the automatic loading class, PSR-0 encoding specification, now share to everyone, the need for friends can refer to
First, the namespace: solve the problem of the class name or function name conflict in the production environment when there are many cooperative development;test1.php
test2.php
test.php
Second, automatic loading class: To solve the problem of introducing too many d
How should I understand the {code...} line of code in the loadClass () method in the implementation of the PSR-4 instance class? I think it should be: {code ...} the problem has been solved: see 'notice 'in the PHP operator priority Manual: although the priority is lower than that of most other operators, PHP still... implementation of PSR-4 instance class
loadClass()Method
while (false !== $pos = strrpos(
"Three conventions of the PSR-0 specification":① namespaces must be consistent with absolute pathsThe first letter of the ② class name must be capitalized③ except for the entry file, the ". PHP" must have only one class (cannot have executable code)"Developing an infrastructure that complies with the PSR-0 specification":① all use namespaces② all PHP files must be loaded automatically and cannot have Includ
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.