Php-composer-php Composer Package management and class automatic onboarding What's the connection? The more detailed the better.

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

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

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

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

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