Automatic loading mode based on PHP specification (composer configuration)

Source: Internet
Author: User
Tags autoload

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)

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.