When we were using PHP components, there was an automatic loading function that caught our attention. It can be said that automatic loading based on demand is much faster than manual uploading. In this article, we will introduce the on-demand loading method of Composer in PHP. Before that, we need to have a simple understanding of Composer. The following will bring detailed content display.
1. Description
Composer will automatically generate PSR-compliant autoloaders for all PHP components in the project, effectively abstracting dependency management and automatic loading. Therefore, Composer is the most important additional tool for the PHP community. , and before we implement automatic loading manually, we also need tools such as include, require, spl_autoload_register, etc. This is not too much.
2. Example
PSR-4 can be implemented through composer (in theory, all PSR standards can be automatically implemented through composer commands). composer.json information is as follows:
{ "name": "lesliexiong/php-server", "description": "server", "authors": [ { "name": "layne", "email": "[email protected]" } ], "require": { "php": ">=5.4.0" }, "autoload": { "psr-4":{ "Layne\Taobao\": "src/" } } }
Then put TestA.php and TestB.php in the src directory, and finally execute composer install. All on-demand code will be automatically generated. Afterwards, anyone can access the encapsulated TestA.php and TestB.php.
The above is the on-demand loading method of Composer in PHP. For those who have not been exposed to Composer, you can first learn the basic content of Composer, and then perform operations on on-demand loading.