For the programming language of PHP, the current fig specifies five specifications, respectively, as follows:
- PSR0: Automatic loading;
- PSR1: Basic code specification;
- PSR2: Code style specification;
- PSR3: Log interface Specification;
- PSR4: automatic loading specification;
It seems that PSR4 and PSR0 are repeated, but the PSR4 specification is relatively clean and can be seen as an upgraded version of the PSR0 specification. The most important difference is that the underscore is converted to a directory separator in the PSR0 specification, but the PSR4 underline does not have a special meaning. Both are implemented by a specific directory, file name, and class name to quickly find the class file and load the corresponding class into it.
PSR0 and PSR4 require a named space, the above Classtest class to make corresponding changes as follows:
<? phpnamespace classtestlib; class classtest{ publicfunction Test () { echo "Hello world!" ; }}
Then the path of the corresponding file should be changed to \lib\classtestlib\classtest.php, at this time modify the AutoLoad in Composer.json as follows:
"AutoLoad":{ "psr-0":{ "classtestlib": "lib/" } }
Maybe you find the value of Psr0 some strange, yes. Here Classtestlib represents the namespace, and "Lib" is the directory name. When loading the corresponding class file, the search path is lib/classtestlib, not classtestlib/lib, which is a point to note when writing Composer.json.
If "\" is present in the namespace, then a "\" is added to the corresponding "\" when writing the corresponding Composer.json. For example, if the namespace is changed to Classtest\lib, the corresponding path name corresponding to the application root should become \lib\classtest\lib\classtest.php, and the AutoLoad in the corresponding Composer.json should become:
"AutoLoad":{ "psr-0":{ "classtest\\lib": "lib/" } }
Ext.: https://www.cnblogs.com/yue-blog/p/5904275.html
Automatic loading mode based on PHP specification (composer configuration)