Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Example Analysis of Composer autoload

Shulou Source: shulou.com Published: 2022-06-02 13:34:28 10月02日 Update

Editor to share with you the example analysis of Composer autoload, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

It is well known that composer is the cornerstone of modern PHP projects. Different from the ancient pear, composer is not a package management system focused on system-level php management, but a project-based library management system. This is like the difference between npm install-g and npm install. And the most important thing is that pear can not keep up with the trend of the times. Pear still has its own way when everyone is using psr-*.

Well, maybe that's a good thing, but it's also a bad thing. The good news is that many good packages have become rich from pear families, such as PHP_CodeSniffer, PHP_Unit and so on. However, with the development of the times, the php community has gradually drawn some essence from other communities and slowly moved forward. The most recent laravel is thrown directly into the composer. Because the psr-4 specification really can't be any better. This is really a set of naming conventions that I use most easily with all kinds of bags.

Go too far, pull back to the core composer file vendor/composer/autoload_real.php.

Types that are loaded automatically

In general, composer provides several types of automatic loading

Classmap

Psr-0

Psr-4

Files

In theory, the project code is loaded automatically with psr-4, helper is loaded automatically with files, and development is loaded automatically with classmap. Psr-0 has been abandoned, but some historical relics are still in use, so I see it occasionally.

Classmap

This should be the simplest autoload mode. It probably means something like this:

{"classmap": ["src/"]}

Then composer reads all the files in this folder behind it, and then angrily generates all the namespace + classname of class into a php array of key = > value in vendor/composer/autoload_classmap.php.

Then you can use the spl_autoload_register function openly to do autoload.

Well, the above example is actually a little bit tricky, that is, the above autoload is actually generated based on prs-4. But it doesn't matter. If you understand the important points at the bottom, we can see that all the so-called autoloading can actually be understood as generating such a classmap, which is what composer dump-autoload-o does. Otherwise, compoesr will read the contents of psr-4 and prs-0 dynamically.

Psr-0

Now this standard is out of date. This standard was developed mainly when php jumped from 5.2 to 5.3 + with the concept of namespaces. So at this time, the standard of psr-0 mainly takes into account

Tags: File that is time build global content folder standard writing document article type or is this project example analysis important thing method Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Shulou Technology Microsoft Docker Xiaomi