What is the connection between php-composer-PHPcomposer package management and automatic class loading? The more detailed, the better.

Source: Internet
Author: User
Tags autoload
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.

  1. 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)

  2. 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.

  3. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.