review

Initially, if you want to use classes or methods from other files in a PHP file, you need to include the file through the include/require methods. The disadvantages of this approach are also obvious:

  1. If you need to import a lot of files, you will need a lot of include statements, which are not only ugly, but also inevitably missed
  2. If a constant with the same name is defined in multiple files, a double-defined warning will be thrown

autoload

To solve this problem, the concept of autoloading was introduced in PHP5, via the __autoload function, as follows:

function __autoload($classname){
	// Complete the loading task for the specified class
	include_once($classname.'.php')}Copy the code

We define this function in a file, and when we come across an unimported class, we call this function to import it, which seems like a good idea, but we just need to define this function in our PHP file, and then we just need to import this auto-loading file for every file, and we’re done.

However, there are many problems with this approach:

  1. Because PHP cannot have functions with the same name, an error is reported when two autoloading functions appear. Of course, our own project can guarantee that, but we still need to introduce third party library.
  2. Putting all function mappings into a single function is bound to result in bloated functions, which are also bad for maintenance

The problem, obviously, is that this is a global function that can only be defined once,

spl_autoload

So how to solve this problem? PHP has introduced an extension library that can define multiple auto-loading functions. When searching, the auto-loading functions defined will be called in sequence to load, including the following methods:

  1. Spl_autoload_register: Registers automatic loading functions
  2. Spl_autoload_unregister: Deletes registered autoload functions
  3. Spl_autoload_functions: Gets all registered autoload functions
  4. Spl_autoload_call: Calls all registered autoload functions in turn for loading
  5. Spl_autoload: Default implementation of the autoload function, which is called by default if not registered
  6. Spl_autoload_extionsions: Register and return the default file extension used in SPL_Autoload

With this, we can define multiple auto-loading functions.

Here’s an example:

test01.php

namespace test01;
class test{
    public static function tt(){
        echo 'test01'; }}Copy the code

test02.php

namespace test02;
class test{
    public static function tt(){
        echo 'test02'; }}Copy the code

run.php

spl_autoload_register(function ($classname){
    include_once $classname.'01.php';
});
spl_autoload_register(function ($classname){
    include_once $classname.'02.php';
});

use test01\test;
test::tt();
Copy the code

Error: Run.php

Warning: include_once(test01\test01.php): failed to open stream: No such file or directory in

Print the passed $classname and find: test01\test

In other words, we can use namespaces that correspond to paths, but there’s a standard for that, I don’t know.

Such a correspondence, there is no feeling and Java package some of the same, anyway I think it is very similar.

composer

Ok, now that the third-party library is managed by Composer, it’s all done with auto-loading, just import its autoload.php file.

Composer provides several types of auto-loading

1.psr-4

It’s basically what we said above about paths and namespaces

2. classmap

Save a map of each class and file


Wait, there is no specific research, but it is basically an auto-loading package, very convenient.

To sum up briefly, talent and learning are shallow..