Follow the PSR-4 auto load, follow the PSR-4 load _ PHP Tutorial

Source: Internet
Author: User
Tags autoload
Follow the automatic loading of the PSR-4 and follow the PSR-4 loading. Follow the PSR-4 of automatic loading, follow the PSR-4 loading, first of all here to understand the PSR-4, ProposingaStandardsRecommendation (put forward standard recommendations) abbreviation, is a PHP development rules follow the of automatic loading, load following PSR-4
I. INTRODUCTION

First of all, we need to understand this section. the abbreviation of Proposing a Standards Recommendation (propose standard recommendations) is a PHP development specification, which makes the code we developed more reasonable, better maintained, and more readable. The following standards are available for the DSRs:

  • PSR-0: auto load
  • PSR-1: basic code specification
  • PSR-2: code style
  • PSR-3: log interface
  • PSR-4: path issues for specification auto-loading

Here we can see that the subscript of the DSRs also starts from 0, and there is also a point like ~ in the array ~. In fact, PSR-4 and PSR-0 is a bit similar or even redundant, they all indicate that the specification of automatic loading, but the specification in the PSR-4 is more concise, in a PSR-0, the underline "_" has a special meaning, and the underline needs to be converted to a directory separator during autoload processing, while the underline does not have any special meaning in the PSR-4, therefore, it is more concise and clearer when files are automatically loaded.

I have made a rough translation of the example in the psr-4 specification above github (I believe your English level must be better than me, certainly can understand ^_^ ), then I made a small example using this automatically loaded class library. The example file is too many and long. it is not suitable here. so I will introduce this example in my blog, for details, go to my github homepage to see this example.

II. Introduction to automatic loading of class libraries

First, let's take a look at the approximate content of the automatically loaded class:

Class Autoload {// register the automatic loading function to the spl autoload stack. public function register (); // add a directory to a namespace prefix. public function addNamespace ($ prefix, $ base_dir, $ prepend = false); // automatically loads a function, the public function loadClass ($ class) will be used in $ this-> register; // find the ing file public function loadMappedFile ($ prefix, $ relative_class ); // check whether a file contains the public function requireFile ($ file) in the file system );}

These functions are used to automatically load class library functions. The register (), addNamespace (), loadMappedFile (), and requireFile () functions are simple and easy to understand at a glance, the only function that may need to be explained is the loadClass function. First, let's look at the code of the loadClass () function:

1 public function loadClass ($ class) 2 {3 // The current namespace prefix 4 $ prefix = $ class; 5 6 // use the namespace to find the corresponding file 7 while (false! ==$ Pos = strrpos ($ prefix, '\') {8 9 // possible namespace prefix 10 $ prefix = substr ($ class, 0, $ pos + 1); 11 12 // the remaining part is a possible class 13 $ relative_class = substr ($ class, $ pos + 1 ); 14 15 // try to load files corresponding to prefix and relitive class 16 $ mapped_file = $ this-> loadMappedFile ($ prefix, $ relative_class); 17 if ($ mapped_file) {18 return $ mapped_file; 19} 20 21 // Move the namespace and relative class to the next location 22 $ prefix = rtrim ($ prefix ,'\\'); 23} 24 25 // the file to be loaded is not found 26 return false; 27}

In fact, there may be only one doubt, that is why we need to recyclically try to find the file. in the while loop, it will gradually shorten the namespace prefix name to find the appropriate namespace prefix. why?

To include more content in the namespace, you do not need to add a new namespace prefix every time you create a folder in the parent namespace, as described in the following figure:

III. example

Here, you may have a better understanding of the automatically loaded content. at this time, let's take a look at the small examples I have prepared. here we will only introduce the directory structure of the small example, because it is relatively simple, the detailed content will not be listed here. if you are interested, you can go to my github homepage to see this example.

-- Core

-Autoload. php

-- Vendor

-- Test1

-Hello. php

-- Test2

-World. php

-App. php

The copyright of this article belongs to the author (luluyrt@163.com) and the blog Park, without the author's consent to prohibit any form of Reprint, repost the article must be in the obvious position on the article page to give the author and the original connection, otherwise, you are entitled to pursue legal liability.

Summary 1. First of all, we need to understand this section. the abbreviation of Proposing a Standards Recommendation (Proposing standard recommendations) is a PHP development rule...

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.