What is the relationship between PHPcomposer package management and automatic class loading? What is the relationship between PHP composer package management and automatic class loading? The more detailed, the better.
Reply content:
What is the connection between PHP composer package management and automatic class loading? The more detailed, the better.
Essentially, the two are not highly correlated.
Automatic loading is the implementation of the Automatic Loading Function registered by spl_autoload_register (this function can also be implemented by reloading the _ autoload function, but it is not recommended that there be only one autoload function ), you can write this function by yourself. Generally, You need to include the parsing of namespace and class names. All registered automatic loading functions are triggered in sequence when PHP cannot find a class. (The function here is a callable object, including: class methods (Dynamic and Static), Closure (Lambda function), and common functions)
Composer manages dependencies between packages through the require configuration provided by composer. json of each package, and provides an autoload implementation to load the classes under these packages.
As long as your code's class storage path is planned according to PSR-4 standards (recommended) and PSR-0 standards, you can use composer's autoload to manage your own classes, just go through the composer documentation.
Therefore, composer is a package manager that manages packages rather than classes. However, it provides an autoload implementation for loading classes in these packages, but it does not prevent you from implementing your own.
Spl_autoload_register: http://php.net/manual/zh/function.spl-autoload-register.php
PSR-4: http://www.php-fig.org/psr/psr-4/ Chinese: https://github.com/PizzaLiu/PHP-FIG/blob/master/PSR-4-autoloader-cn.md
PSR-0: http://www.php-fig.org/psr/psr-0/ Chinese: https://github.com/PizzaLiu/PHP-FIG/blob/master/PSR-0-cn.md
Totally two things
Composer installs your dependent projects from the source. For example, many packages in Linux are mutually dependent. When package A and package B are used, B will also install.
Automatic class loading means that you are unlikely to use only one file or class to complete all services. For example, in the MVC Architecture, different Controllers may need to call each other, controllerA. the PHP file does not contain controllerB. php code can only be implemented through automatic loading of classes. set controllerB. php loads the context into.
Here is an article about the PHP auto-loading mechanism. You can refer to the PHP auto-loading tool.