An example of a file that follows PSR-4 AutoLoad

Source: Internet
Author: User
Tags autoload
First, Introduction

First of all, here to understand psr,proposing a standards recommendation (the standard recommendation) abbreviation, is a PHP development specification, let us develop the code more reasonable, better maintenance, more readable. There are several criteria for PSR:

    • PSR-0: Auto Load
    • PSR-1: Basic code specification
    • PSR-2: Code Style
    • PSR-3: Log interface
    • PSR-4: Routing problem for spec auto-loading

Here we see that the subscript for PSR is also starting from 0, and the array is a bit like ~. In fact, PSR-4 and PSR-0 are somewhat similar or even redundant, they are all explained is the automatic loading specification, but PSR-4 in the specification is more concise, in the PSR-0 underlined "_" is a special meaning, in the autoload processing need to convert the underscore to the directory separator, And in the PSR-4 underline is not any special meaning, so in the file automatically loaded when the more concise, more clear conditioning.

I have made a general translation of the examples in the PSR-4 specification above GitHub (I believe your English level must be better than me, you can certainly read ^_^), and then use this automatic loading class library to do a small example, the example file many, long, put here is not suitable, So I'm going to introduce this example in my blog, and I'd like to know more about this example from my GitHub homepage.

Second, automatic loading class library Introduction

First look at the approximate contents of the auto-load class:

class Autoload {  //  Register the auto-load function into the SPL Autoload stack.  Public function    Register ();  //  Add a directory to a namespace prefix in publicfunction addnamespace ($prefix $base _dir    $prepend=false);  //  Auto-load function, the publicfunction loadclass is used in $this->register ( $class    );  //  look for mapped files publicfunction loadmappedfile ($prefix $relative _class    );  // See if a file exists in the file system publicfunction requirefile ($file  ); }                     

The automatic loading of class library functions in these functions, where register (), AddNamespace (), Loadmappedfile (), Requirefile () functions are relatively simple, one can understand, The only function that may need to be explained is the LoadClass function, which first looks at the code of the LoadClass () function:

1      Public functionLoadClass ($class)2     {3         //the current namespace prefix4         $prefix=$class;5         6         //to find the corresponding file by namespace7          while(false!==$pos=Strrpos($prefix, '\\')) {8             9             //namespace prefixes that may existTen             $prefix=substr($class, 0,$pos+ 1); One  A             //the remainder is a class that may exist -             $relative _class=substr($class,$pos+ 1); -  the             //attempting to load the prefix prefix and relitive class corresponding file -             $mapped _file=$this->loadmappedfile ($prefix,$relative _class); -             if($mapped _file) { -                 return $mapped _file; +             } -  +             //move namespace and relative class split position to next position A             $prefix=RTrim($prefix, '\\');  at         } -          -         //the file you tried to load was not found -         return false; -}

In fact, there may be a doubt that there is only one place, that is why the loop to try to find the file, in the while loop, will slowly shorten the name of the namespace prefix to find the appropriate namespace prefix, why do you want to do so?

The loop looks for a file to contain more content in the namespace, without having to add a new namespace prefix each time you create a new folder in the parent namespace, as described in the following figure:

When a file is under a subdirectory in a namespace, we do not have to create a new namespace prefix to successfully load the required files, and maintain namespace prefixes with fewer arrays and better maintenance. Conversely, if there is no loop lookup, this is the way it looks.

  

Each time a new subdirectory will go to a new namespace prefix, is not very troublesome, but there is a certain advantage, that is, when loading the loop to find files, may reduce a certain amount of time consumption, but it is a bit cumbersome to load.

Therefore, it is more convenient to load this method in a loop, but it is important to not let the directory hierarchy with no namespace prefixes be too deep, which consumes unnecessary time to file loading. When the need for high efficiency, and our directory is certainly not uncertain, this time when loading to remove the loop lookup, but to add a namespace for each directory, the efficiency may be improved, just a little bit of my humble opinion.

Iii. examples

Said here you may have been on the automatic loading of the content, this time to see my small example of the preparation, here is just a small example of the directory structure, because the relatively simple, detailed content is no longer listed here, interested Tongxu can go to my GitHub homepage to see this example

--core

-autoload.php

--vendor

--test1

-hello.php

--test2

-world.php

-app.php

This article copyright belongs to the author (luluyrt@163.com) and the blog park, without the author's consent to prohibit any form of reprint, reproduced article must be in the article page obvious location to the author and the original link, otherwise reserves the right to pursue legal responsibility.

The above describes a follow PSR-4 file AutoLoad example, including the aspects of the content, I hope that the PHP tutorial interested in a friend to help.

  • 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.