A lot of small partners in, learning PHP when one of the first problems is require, include and require_once, Include_once love to kill.
After knowing the story of their love and killing, they often begin to use the framework. The framework is a good tool for work, but do you know what happens when you are new to a class? Have you ever wondered why we are automatically doing everything we need to do when we follow the code? Let us all explore the mysteries of discovering them.
Time Line
Steam Age
At the top of the PHP code, you are not often seeing such code.
Require ' lionis.php '; require ' is.php '; require ' cool.php ';
If you just introduce a few PHP scripts, that's acceptable. The explosion was unavoidable when thousands of scripts were introduced. If you change a script name, you also need to rename each script that introduced the script, can it not explode? How do you get around this conversation?
Electrical Age
In the era of the PHP electrical, the __autoload and Spl_autoload_register functions were started to register their own custom automatic loading strategies.
In layman's terms, __autoload and Spl_autoload_register are a killer organization, and they are going to hire national killers (functions). When we want to fix someone (new), we just need to provide a name (the class name), and the rest of the killers will help us get it done.
__autoload
PHP 5 starts with this function portal. When you use a class that cannot be found, it throws the class name into the function as a parameter.
Output
Oyeje, I'm lionis.
Spl_autoload_register
If our project is very old or you are a love tossing the young pioneers, need to introduce things have different specifications, this time if all in the __autoload function, this function will immediately swell. And __autoload is globally unique and can cause errors if it is occupied by someone. (To make a man perish, it will swell first.) )
PHP 5.1.2 begins to provide this function portal, registering a given function as the implementation of the __autoload. Therefore, we look at some frameworks or plugins in their own use, in order to be compatible may appear function_exists (spl_autoload_register).
Oh yes, we can write a lot of different auto-loading functions.
Information Age
Master careful, there are yaoqi in front! If each of us implements a set of automatic loading methods, each PHP component and framework uses a unique autoloader, and each framework uses different logic to load PHP classes, interfaces, and traits.
So when we use some third-party frameworks, we also need to figure out the bootloader in the boot file, and that's a lot of time. Php-fig aware of this problem, it is recommended to use the PSR-4 specification to facilitate interoperability between components so that we can use an autoloader.
PSR-4 specification
Use the prefix of the namespace to correspond to the directory in the file system.
The mapping relationship is
namespace = Filepath\lionis\cool = Cool
Classes with namespaces
Create an Object
At this time, according to PSR-4 specification, the autoloader should go to load the real.php in the cool/directory.
Wrong! That's not the way to implement the autoloader itself, or how can there be an automatic loader? Is the official built-in?
You're out, we can use the dependency manager composer to build the PSR-4 autoloader. You might be wondering, what if my old project didn't follow the PSR-4 specification? Hey, let's explore and discover how composer solves this problem.
Composer
Oh roar, this time we focus on the automatic loading, so the installation and use of composer, etc., will not be discussed.
Composer Automatic loading is set up with 4 modes of loading:
PSR-0
PSR-4
Classmap
Files
PSR-0
The namespace and directory layers are required, and you can use _ as the path delimiter, but this causes the catalog results to become too deep.
When composer performs an install operation, composer stores the configuration in the file in the returned array in the vendor/composer/autoload_psr0.php file.
For example: Very\good=>vendor\lionis\isreal\cool is defined, and the actual directory loaded with the call to use very\good\love\someclass,psr-0 is Vendor/lionis/isreal /cool/very/good/love/someclass.php.
Yes, it was really scary, so PSR-0 was officially abolished. But some mainstream frameworks have been PSR-0, in order to be backward-compatible or to achieve PSR-0.
Composer.json configuration:
"AutoLoad": { "psr-0": { "Very\\good": "Vendor\lionis\isreal\cool" }}
PSR-4
PSR-4 is now a more recommended method for replacing PSR-0.
Unlike PSR-0, it cancels out _ as a delimiter and directory structure.
When composer performs an install operation, composer stores the configuration in the file in the returned array in the vendor/composer/autoload_psr4.php file.
For example: Define Very\good=>vendor\lionis\isreal\cool, call the use very\good\
The actual directory loaded by Love\someclass,psr-4 is vendor/lionis/isreal/cool/love/someclass.php.
Composer.json configuration:
"AutoLoad": { "psr-4": { "Very\\good": "Vendor\lionis\isreal\cool" }}
Classmap
Classmap by configuring the specified directories and files, when composer performs an install operation, composer scans the class in the corresponding directory at the end of the. php file and stores it in the Vendor/composer/autoload_ The returned array in the classmap.php file.
Composer.json configuration:
"AutoLoad": { "Classmap": [ "lionis/", "xiaoer/" ]}
If there is a file called Verycool under Lionis, it will be generated in vendor/composer/autoload_classmap.php.
$baseDir. '/lionis/verycool.php ', //other mappings);
Files
Files is a straightforward and crude loading file. When performing an install operation such as composer, composer generates a $files array in the file with the configuration stored in the vendor/composer/autoload_static.php file.
Composer.json configuration:
"AutoLoad": { "files": ["lionis/very/cool.php"]}