What is the connection between PHP Composer package management and class auto onboarding? The more detailed the better.
Reply content:
What is the connection between PHP Composer package management and class auto onboarding? The more detailed the better.
In essence, they are not very relevant.
Automatic loading is an automatic load function implementation of Spl_autoload_register registration (by overloading the __autoload function, but not recommended, so there can be only one autoload function), you can write the function yourself, It is generally necessary to include parsing the namespace and class names and include them in. All registered auto-loading functions are triggered sequentially when PHP cannot find a class. (The function here is a callable object, including: Class method (static and dynamic), closure closure (lambda function), normal function)
Composer manages the dependencies between packages through the configuration of the require provided by the Composer.json of each package, and at the same time it provides a autoload implementation to load the classes under these packages
As long as your code's class storage path is planned according to the PSR-4 Standard (recommended) and the PSR-0 standard, you can use composer's autoload to manage your own classes, which is a bit of a document to go over composer
So composer is a package manager that manages packages instead of classes, but provides a autoload implementation that loads the classes within these packages, but does not prevent you from implementing one yourself.
spl_autoload_register:http://php.net/manual/zh/function.spl-autoload-register.php
PSR-4: http://www.php-fig.org/psr/psr-4/Han: https://github.com/PizzaLiu/PHP-FIG/blob/master/PSR-4-autoloader-cn.md
PSR-0: http://www.php-fig.org/psr/psr-0/Han: https://github.com/PizzaLiu/PHP-FIG/blob/master/PSR-0-cn.md
Totally two things.
Composer is to install the project you depend on from the source, for example, the Linux system has many packages that are interdependent, and B will be installed when package B is available.
Class Auto-loading is that you are not likely to use only one file or class to finish all the business, such as the MVC architecture, different controllers may want to call each other, the controllera.php file does not have controllerb.php code, Can only be implemented by automatic loading of classes. Loads the controllerb.php into the context of a.
Here is a tutorial on PHP automatic loading mechanism can see PHP autoloader