PHP Autoload Auto Load class

Source: Internet
Author: User
Tags autoload autoloader
We know that PHP can be automatically loaded, avoid the heavy manual work, code more standardized, neat. So if we take this auto-load and sublimate it into an auto-load class, we just need to introduce this class each time, then the other classes are loaded automatically. At the same time, if the load can make it resident memory, then the efficiency is high ...

About the basic part of the automatic in the previous blog ();

Here are a few things to note

1. In order to achieve commonality, set up a root directory, in large projects, the directory structure is more complex, each load should have a starting directory location, so that backward compatibility. Of course, you can set more than one of these directories and put them in the array.

2. Classes with namespaces need to convert the escape character to a slash that represents the directory structure, and the loaded class is with a namespace because the class belongs to that namespace. (as you can tell from the previous blog post)

Okay, here's the code.

1
    

The use of automatic loading must show load, it is loaded with other class loaders, we have overloaded PHP automatically loaded without mechanism can load this Autoloader, so to require_once (' autoloader.php ');

It's simple, but it can be more powerful.

For example, I made this into a middleware, named Bootstrap, which can be used for different projects, which can be located in different or the same Web directory, when a static property is required to represent all possible application directories or different web directories

1 public static $rootPath = Array ();

At load time, a loop is required to iterate through each of the possible directories to find the load.

At the same time, we can set a Setrootpath () method to set the directory for different projects.

Public Function Setrootpath ($path) {        //do something        return $this;}

This will enable the chain operation to add, the personal feel more magical ...

OK, about the automatic loading is written here, welcome to watch the shooting bricks.

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